-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
81 lines (63 loc) · 1.65 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"errors"
"fmt"
"os"
)
var _ error = (*SQLError)(nil)
type SQLError struct {
message string
}
func (de SQLError) Error() string {
return de.message
}
func newSQLError(message string) *SQLError {
return &SQLError{message: message}
}
var (
ErrSQLNoRows = newSQLError("no rows error")
)
// Let's force an error from the nested function call chain that simulate a database call.
// main() -> getData() -> decodeData() -> fetchDataFromMockDB()
func fetchDataFromMockDB() (string, error) {
return "", ErrSQLNoRows
}
func decodeData() (string, error) {
data, err := fetchDataFromMockDB()
if err != nil {
return "", fmt.Errorf("decode data error: %w", err)
}
return data, nil
}
func getData() (string, error) {
data, err := decodeData()
if err != nil {
return "", fmt.Errorf("get data error: %w", err)
}
return data, nil
}
func main() {
data, err := getData()
if err != nil {
// err is ErrSQLNoRows but wrapped. So the following won't work.
// err == ErrSQLNoRows yields false.
// Use errors.Is() to compare ErrSQLNoRows against the wrapped errors.
if errors.Is(err, ErrSQLNoRows) {
fmt.Println("err is ErrSQLNoRows")
}
// For errors.As(), the second parameter is a pointer to pointer type.
var targetErr *SQLError
if errors.As(err, &targetErr) {
fmt.Println("err is a SQLError")
}
// Output format: topmost error message -> ... -> bottommost error message
// Output: get data error: decode data error: no rows error
fmt.Println(err)
// Unwrap the previous error.
// Output: decode data error: no rows error
unwrapErr := errors.Unwrap(err)
fmt.Println(unwrapErr)
os.Exit(1)
}
fmt.Println(data)
}