Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
174 lines (144 loc) · 4.42 KB
/
Copy pathexample_test.go
File metadata and controls
174 lines (144 loc) · 4.42 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package errors_test
import (
"fmt"
"io"
"log/slog"
"os"
"slices"
"github.com/gravitton/errors"
)
var ErrNotFound = errors.Sentinel("not found")
func newLogger() *slog.Logger {
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
if len(groups) == 0 && attr.Key == slog.TimeKey {
return slog.Attr{}
}
return attr
},
}))
}
func ExampleNew() {
err := errors.New("boom")
fmt.Printf("%v\n", err)
fmt.Printf("%q\n", err)
// Output:
// boom
// "boom"
}
func ExampleNewf() {
err := errors.Newf("load user %d: %w", 42, ErrNotFound)
fmt.Println(err)
// Output:
// load user 42: not found
}
func ExampleWrap() {
inner := errors.New("connection refused")
err := errors.Wrap(fmt.Errorf("load config: %w", inner))
fmt.Println(err)
fmt.Println(slices.Equal(err.StackTrace(), inner.StackTrace()))
// Output:
// load config: connection refused
// true
}
func ExampleError_Fields() {
err := errors.Wrap(ErrNotFound).WithFields(map[string]any{"id": 42, "table": "users"})
err = err.WithField("table", "accounts")
fmt.Println(err)
fmt.Println(err.Fields())
// Output:
// not found
// map[id:42 table:accounts]
}
func ExampleError_WithCause() {
cause := errors.New("connection refused").WithField("port", 5432)
err := errors.New("could not load config").WithCause(cause).WithCause(io.ErrClosedPipe)
fmt.Println(err)
fmt.Println(errors.Is(err, io.ErrClosedPipe))
// Output:
// could not load config
// true
}
func ExampleError_Format() {
err := errors.New("could not load config").WithField("path", "config.yml").WithCause(io.EOF)
fmt.Printf("%+v\n", err)
// The stack trace holds file paths that depend on where the module is built, so this
// example has no checked output. It prints something like:
//
// could not load config
// path=config.yml
// github.com/gravitton/errors_test.ExampleError_Format
// /app/example_test.go:79
// ...
// caused by: EOF
}
func ExampleError_GoString() {
var zero errors.Error
err := errors.New("boom")
fmt.Printf("%#v\n", &zero)
fmt.Printf("%#v\n", err)
fmt.Printf("%#v\n", err.WithField("id", 42))
fmt.Printf("%#v\n", err.WithCause(io.EOF))
// Output:
// &errors.Error{}
// &errors.Error{err:&errors.errorString{s:"boom"}}
// &errors.Error{err:&errors.errorString{s:"boom"}, fields:map[string]interface {}{"id":42}}
// &errors.Error{err:&errors.errorString{s:"boom"}, cause:&errors.errorString{s:"EOF"}}
}
func ExampleError_LogValue() {
cause := errors.New("connection refused").WithField("port", 5432)
err := errors.Wrap(ErrNotFound).WithField("id", 42).WithCause(cause)
newLogger().Error("load user", "err", err)
// Output:
// level=ERROR msg="load user" err.msg="not found" err.fields.id=42 err.cause.msg="connection refused" err.cause.fields.port=5432
}
func ExampleMultiError() {
errs := errors.NewMulti()
fmt.Println(errs.ErrorOrNil())
errs.Add(nil, io.EOF)
errs.Add(errors.Join(io.ErrUnexpectedEOF, io.ErrClosedPipe))
fmt.Printf("%v\n", errs)
// Output:
// <nil>
// EOF
// unexpected EOF
// io: read/write on closed pipe
}
func ExampleJoin() {
fmt.Println(errors.Join(nil, nil))
fmt.Println(errors.Join(io.EOF, nil))
fmt.Println(errors.Join(io.EOF, errors.New("line one\nline two"), io.ErrClosedPipe))
// Output:
// <nil>
// EOF
// EOF
// line one
// line two
// io: read/write on closed pipe
}
func ExampleMultiError_Format() {
errs := errors.Join(errors.New("connection refused").WithField("port", 5432), io.EOF)
fmt.Printf("%+v\n", errs)
// The stack trace holds file paths that depend on where the module is built, so this
// example has no checked output. It prints something like:
//
// connection refused
// port=5432
// github.com/gravitton/errors_test.ExampleMultiError_Format
// /app/example_test.go:146
// ...
// EOF
}
func ExampleMultiError_GoString() {
fmt.Printf("%#v\n", errors.NewMulti())
fmt.Printf("%#v\n", errors.Join(io.EOF, errors.New("boom").WithField("id", 42)))
// Output:
// &errors.MultiError{}
// &errors.MultiError{errs:[]error{&errors.errorString{s:"EOF"}, &errors.Error{err:&errors.errorString{s:"boom"}, fields:map[string]interface {}{"id":42}}}}
}
func ExampleMultiError_LogValue() {
errs := errors.Join(errors.New("connection refused").WithField("port", 5432), io.EOF)
newLogger().Error("load users", "err", errs)
// Output:
// level=ERROR msg="load users" err.0.msg="connection refused" err.0.fields.port=5432 err.1=EOF
}