mirror of
https://salsa.debian.org/mdosch/go-sendxmpp
synced 2024-11-18 21:25:31 +00:00
Start wrapping errors.
This commit is contained in:
parent
4664c02cbc
commit
ea4b1a7de4
@ -28,22 +28,22 @@ func validUTF8(s string) string {
|
||||
func validURI(s string) (*url.URL, error) {
|
||||
// Check if URI is valid
|
||||
uri, err := url.ParseRequestURI(s)
|
||||
return uri, err
|
||||
return uri, fmt.Errorf("validURI: %w", err)
|
||||
}
|
||||
|
||||
func readFile(path string) (*bytes.Buffer, error) {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("readFile: %w", err)
|
||||
}
|
||||
buffer := new(bytes.Buffer)
|
||||
_, err = buffer.ReadFrom(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("readFile: %w", err)
|
||||
}
|
||||
err = file.Close()
|
||||
if err != nil {
|
||||
fmt.Println(" error while closing file:", err)
|
||||
fmt.Println("error while closing file:", err)
|
||||
}
|
||||
return buffer, nil
|
||||
}
|
||||
|
11
main.go
11
main.go
@ -7,6 +7,7 @@ package main
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
@ -38,13 +39,13 @@ func readMessage(messageFilePath string) (string, error) {
|
||||
// Check that message file is existing.
|
||||
_, err = os.Stat(messageFilePath)
|
||||
if os.IsNotExist(err) {
|
||||
return output, err
|
||||
return output, fmt.Errorf("readMessage: %w", err)
|
||||
}
|
||||
|
||||
// Open message file.
|
||||
file, err := os.Open(messageFilePath)
|
||||
if err != nil {
|
||||
return output, err
|
||||
return output, fmt.Errorf("readMessage: %w", err)
|
||||
}
|
||||
scanner := bufio.NewScanner(file)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
@ -56,9 +57,9 @@ func readMessage(messageFilePath string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
if err := scanner.Err(); errors.Unwrap(err) != nil {
|
||||
if err != io.EOF {
|
||||
return "", err
|
||||
return "", fmt.Errorf("readMessage: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +68,7 @@ func readMessage(messageFilePath string) (string, error) {
|
||||
fmt.Println("error while closing file:", err)
|
||||
}
|
||||
|
||||
return output, err
|
||||
return output, fmt.Errorf("readMessage: %w", err)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
Loading…
Reference in New Issue
Block a user