Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
109 lines (82 loc) · 1.82 KB
/
Copy pathexample_test.go
File metadata and controls
109 lines (82 loc) · 1.82 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package serror
import (
"errors"
"fmt"
)
func ExampleNew() {
err := New("something went wrong")
fmt.Println(err.Error())
// Output:
// something went wrong
}
func ExampleNew_withAttrs() {
err := New("something went wrong", "user", "alice", "attempt", 3)
e := err.(*Error)
fmt.Println(e.Error())
attrs := e.Attrs()
fmt.Println(attrs["user"])
fmt.Println(attrs["attempt"])
// Output:
// something went wrong
// alice
// 3
}
func ExampleWrap() {
cause := errors.New("disk failure")
err := Wrap(cause, "failed to read file")
fmt.Println(err.Error())
fmt.Println(errors.Is(err, cause))
// Output:
// failed to read file
// true
}
func ExampleWrap_noMessage() {
cause := errors.New("disk failure")
err := Wrap(cause)
fmt.Println(err.Error())
fmt.Println(errors.Is(err, cause))
// Output:
// disk failure
// true
}
func ExampleSentinel() {
const ErrNotFound Sentinel = "not found"
err := Wrap(ErrNotFound, "failed to fetch resource")
fmt.Println(err.Error())
fmt.Println(errors.Is(err, ErrNotFound))
// Output:
// failed to fetch resource
// true
}
func ExampleWrap_serrorError() {
original := New("original error")
wrapped := Wrap(original, "wrapped error")
fmt.Println(wrapped.Error())
fmt.Println(wrapped == original)
// Output:
// wrapped error
// false
}
func ExampleError_Format() {
err := New("test error", "key", "value")
// %v and %s produce the message only.
fmt.Printf("%%v: %v\n", err)
fmt.Printf("%%s: %s\n", err)
// %q quotes the message.
fmt.Printf("%%q: %q\n", err)
// Output:
// %v: test error
// %s: test error
// %q: "test error"
}
func ExampleError_Unwrap() {
cause := errors.New("inner error")
err := Wrap(cause, "outer error")
unwrapped := errors.Unwrap(err)
fmt.Println(unwrapped)
var s *Error
fmt.Println(errors.As(err, &s))
// Output:
// inner error
// true
}