-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
74 lines (58 loc) · 1.7 KB
/
example_test.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
package errors_test
import (
"fmt"
"os"
"github.com/VKoptev/go-errors"
)
// Imagine that it's public errors of your useful_package.
var (
ErrSomethingWentWrong = errors.WithReason("something went completely wrong")
)
// Also there are private error of your useful_package.
var (
errSomethingGoingProbablyWrong = errors.WithReason("something is going probably wrong")
)
func DoSomethingUseful() ([]int, error) {
var result []int
for i := 0; i < 10; i++ {
err := doVeryUsefulPart(i)
if err != nil {
// Checking errors is same that builtin package.
// If got errSomethingGoingProbablyWrong (or error wrapping it) we should skip number.
if errors.Is(err, errSomethingGoingProbablyWrong) {
continue
}
// Otherwise we must return public error to user and... ohgosh! data that produced error. Nice.
return nil, ErrSomethingWentWrong.WithX(i)
}
result = append(result, i)
}
return result, nil
}
func doVeryUsefulPart(i int) error {
if i%2 == 1 { // is it odd number? suspiciously...
return errSomethingGoingProbablyWrong
}
if i == 7 { // amount of Voldemort's horcruxes!
//nolint:golint,lll,goerr113
return fmt.Errorf("I'm Voldemort! I can ignore linter and write very long in-placed errors capitalized and ended with punctuation!")
}
return nil
}
func Example() {
r, err := DoSomethingUseful()
if err != nil {
var terr *errors.Error
if errors.As(err, &terr) {
// If returned error may be matched as *errors.Error
// let's just use it pretty print included wrapped errors and data.
fmt.Println(err.Error())
os.Exit(1)
}
// Otherwise let's form pretty print.
fmt.Printf("Got error: %s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Got data: %v", r)
os.Exit(0)
}