Start wrapping errors.

This commit is contained in:
Martin Dosch 2023-06-06 22:09:59 +02:00
parent 4664c02cbc
commit ea4b1a7de4
No known key found for this signature in database
GPG Key ID: 52A57CFCE13D657D
2 changed files with 10 additions and 9 deletions

View File

@ -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
View File

@ -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() {