-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patherrors_test.go
More file actions
304 lines (263 loc) · 9.88 KB
/
Copy patherrors_test.go
File metadata and controls
304 lines (263 loc) · 9.88 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package errors_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stackus/errors"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
)
// ErrUserNotFound is an application sentinel error. It is a distinct error
// identity that also belongs to the built-in ErrNotFound category, so it maps
// to HTTP 404 and gRPC NotFound.
var ErrUserNotFound = errors.NewKind(
"USER_NOT_FOUND",
errors.ErrNotFound,
"user not found",
errors.WithPublicMessage("The user could not be found"),
)
// errNoRows stands in for a driver error such as sql.ErrNoRows.
var errNoRows = errors.New("sql: no rows in result set")
// This example follows an error from a repository up through a service. The
// repository classifies the driver error at the boundary, the service adds
// context, and the caller reads the codes and checks the error's identity.
func Example() {
findUser := func(id string) error {
// Classify the driver error where it enters the application.
return ErrUserNotFound.Wrap(errNoRows, "find user "+id)
}
getProfile := func(id string) error {
// Add context on the way up; the classification is preserved.
return errors.Wrap(findUser(id), "get profile")
}
err := getProfile("42")
fmt.Println(err)
fmt.Println(errors.TypeCode(err))
fmt.Println(errors.HTTPCode(err))
fmt.Println(errors.GRPCCode(err))
fmt.Println(errors.Is(err, ErrUserNotFound))
fmt.Println(errors.Is(err, errors.ErrNotFound))
fmt.Println(errors.Is(err, errNoRows))
fmt.Println(errors.PublicMessage(err))
// Output:
// get profile: find user 42: sql: no rows in result set
// USER_NOT_FOUND
// 404
// NotFound
// true
// true
// true
// The user could not be found
}
// This example writes an error as an HTTP response. The status comes from
// HTTPCode, and the body is the client-safe code and message from Public.
func Example_httpHandler() {
writeError := func(w http.ResponseWriter, err error) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(errors.HTTPCode(err))
_ = json.NewEncoder(w).Encode(errors.Public(err))
}
handle := func(err error) {
rec := httptest.NewRecorder()
writeError(rec, err)
fmt.Print(rec.Code, " ", rec.Body.String())
}
handle(ErrUserNotFound.Wrap(errNoRows, "find user 42"))
handle(errors.ErrInternal.Wrap(errors.New("dial tcp 10.0.0.5:5432: connection refused"), "load user"))
handle(errors.ErrUnprocessableEntity.Msg("email: missing @"))
// Output:
// 404 {"code":"USER_NOT_FOUND","message":"The user could not be found"}
// 500 {"code":"INTERNAL","message":"Internal Server Error"}
// 422 {"code":"UNPROCESSABLE_ENTITY","message":"Unprocessable Entity"}
}
// Wrapping a built-in category directly uses the message as the complete error
// text. The result still matches the category.
func ExampleWrap() {
err := errors.Wrap(errors.ErrNotFound, "user 42 not found")
fmt.Println(err)
fmt.Println(errors.Is(err, errors.ErrNotFound))
// Output:
// user 42 not found
// true
}
// Each additional Wrap adds another prefix.
func ExampleWrap_multiple() {
err := errors.Wrap(errors.ErrNotFound, "original message")
err = errors.Wrap(err, "prefixed message")
fmt.Println(err)
// Output: prefixed message: original message
}
// Wrapping any other error produces "msg: cause" and keeps the cause and its
// classification reachable.
func ExampleWrap_ordinaryError() {
err := errors.Wrap(errors.ErrForbidden.Msg("project is archived"), "update project")
fmt.Println(err)
fmt.Println(errors.TypeCode(err))
// Output:
// update project: project is archived
// FORBIDDEN
}
// Use a category's Wrap to classify an error that came from somewhere else,
// such as a database driver or another library.
func ExampleError_Wrap() {
err := errors.ErrNotFound.Wrap(errNoRows, "find order 7")
fmt.Println(err)
fmt.Println(errors.HTTPCode(err))
fmt.Println(errors.Is(err, errors.ErrNotFound))
fmt.Println(errors.Is(err, errNoRows))
// Output:
// find order 7: sql: no rows in result set
// 404
// true
// true
}
// Msg creates a classified error from scratch.
func ExampleError_Msg() {
err := errors.ErrInvalidArgument.Msg("page size must be positive")
fmt.Println(err)
fmt.Println(errors.GRPCCode(err))
// Output:
// page size must be positive
// InvalidArgument
}
func ExampleError_Msgf() {
err := errors.ErrTooManyRequests.Msgf("limit of %d requests per minute exceeded", 60)
fmt.Println(err)
fmt.Println(errors.HTTPCode(err))
// Output:
// limit of 60 requests per minute exceeded
// 429
}
// WithCause classifies an error without changing its text.
func ExampleError_WithCause() {
err := errors.ErrUnavailable.WithCause(errors.New("dial tcp: connection refused"))
fmt.Println(err)
fmt.Println(errors.TypeCode(err))
// Output:
// dial tcp: connection refused
// UNAVAILABLE
}
// When a joined error contains different classifications, the join is
// UNKNOWN. Classify the join itself to choose the result.
func ExampleJoin() {
joined := errors.Join(
errors.ErrNotFound.Msg("user not found"),
errors.ErrForbidden.Msg("project is private"),
)
fmt.Println(errors.TypeCode(joined))
err := errors.ErrInvalidArgument.WithCause(joined)
fmt.Println(errors.TypeCode(err))
fmt.Println(errors.Is(err, errors.ErrForbidden))
// Output:
// UNKNOWN
// INVALID_ARGUMENT
// true
}
// AsType finds the first error in the chain that implements an interface, such
// as TypeCoder.
func ExampleAsType() {
err := fmt.Errorf("checkout: %w", errors.ErrConflict.Msg("cart changed"))
if coder, ok := errors.AsType[errors.TypeCoder](err); ok {
fmt.Println(coder.TypeCode())
}
// Output: CONFLICT
}
func TestWrapping(t *testing.T) {
cause := errors.New("no rows found") // simulate sql.ErrNoRows
err := errors.ErrNotFound.Wrap(cause, "find user")
err = fmt.Errorf("repository: %w", err)
err = errors.Wrap(err, "login")
require.True(t, errors.Is(err, errors.ErrNotFound))
require.Equal(t, "NOT_FOUND", errors.TypeCode(err))
require.Equal(t, http.StatusNotFound, errors.HTTPCode(err))
}
func TestClassification(t *testing.T) {
cause := errors.ErrNotFound.Msg("user not found")
err := errors.ErrUnauthorized.Wrap(cause, "unauthorized access")
require.True(t, errors.Is(err, errors.ErrNotFound))
require.True(t, errors.Is(err, errors.ErrUnauthorized))
require.Equal(t, "UNAUTHORIZED", errors.TypeCode(err))
require.Equal(t, http.StatusUnauthorized, errors.HTTPCode(err))
require.Equal(t, codes.Unauthenticated, errors.GRPCCode(err))
}
func TestJoinedErrors(t *testing.T) {
same := errors.Join(
errors.ErrNotFound,
errors.ErrNotFound.Msg("missing record"),
)
require.Equal(t, http.StatusNotFound, errors.HTTPCode(same))
mixed := errors.Join(
errors.ErrNotFound,
errors.ErrForbidden,
)
require.Equal(t, "UNKNOWN", errors.TypeCode(mixed))
require.Equal(t, http.StatusInternalServerError, errors.HTTPCode(mixed))
classified := errors.ErrInternal.Wrap(mixed, "multiple operations failed")
require.Equal(t, "INTERNAL", errors.TypeCode(classified))
require.True(t, errors.Is(classified, errors.ErrNotFound))
require.True(t, errors.Is(classified, errors.ErrForbidden))
}
func TestWrappedCategoryPreservesCause(t *testing.T) {
err := errors.ErrBadRequest.Wrap(errors.ErrForbidden, "some error")
require.Equal(t, http.StatusBadRequest, errors.HTTPCode(err))
require.Equal(t, "some error: FORBIDDEN", err.Error())
require.ErrorIs(t, err, errors.ErrBadRequest)
require.ErrorIs(t, err, errors.ErrForbidden)
}
func TestErrorMessagesAndWrapping(t *testing.T) {
cause := errors.New("record missing")
type testCase struct {
build func() error
message string
wantCause error
classified bool
}
tests := map[string]testCase{
"formatted category message": {func() error { return errors.ErrNotFound.Msgf("record %d", 42) }, "record 42", nil, true},
"category with cause": {func() error { return errors.ErrNotFound.WithCause(cause) }, "record missing", cause, true},
"category wrap without prefix": {func() error { return errors.ErrNotFound.Wrap(cause, "") }, "record missing", cause, true},
"formatted category wrap": {func() error { return errors.ErrNotFound.Wrapf(cause, "lookup %d", 42) }, "lookup 42: record missing", cause, true},
"generic wrap of category": {func() error { return errors.Wrap(errors.ErrNotFound, "lookup") }, "lookup", errors.ErrNotFound, true},
"generic wrap of ordinary error": {func() error { return errors.Wrap(cause, "lookup") }, "lookup: record missing", cause, false},
"formatted generic wrap": {func() error { return errors.Wrapf(cause, "lookup %d", 42) }, "lookup 42: record missing", cause, false},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
err := tt.build()
require.Equal(t, tt.message, err.Error())
require.Equal(t, tt.wantCause, errors.Unwrap(err))
if tt.classified {
require.ErrorIs(t, err, errors.ErrNotFound)
require.Equal(t, "NOT_FOUND", errors.TypeCode(err))
}
})
}
}
func TestErrorWrappingNilAndEmpty(t *testing.T) {
tests := map[string]func() error{
"category WithCause nil": func() error { return errors.ErrNotFound.WithCause(nil) },
"category Wrap nil": func() error { return errors.ErrNotFound.Wrap(nil, "lookup") },
"category Wrapf nil": func() error { return errors.ErrNotFound.Wrapf(nil, "lookup %d", 42) },
"generic Wrap nil": func() error { return errors.Wrap(nil, "lookup") },
"generic Wrapf nil": func() error { return errors.Wrapf(nil, "lookup %d", 42) },
}
for name, call := range tests {
t.Run(name, func(t *testing.T) {
require.NoError(t, call())
})
}
cause := errors.New("record missing")
require.Equal(t, cause, errors.Wrap(cause, ""))
}
func TestErrorAsHelpers(t *testing.T) {
kind := errors.NewKind("MISSING_RECORD", errors.ErrNotFound, "record missing")
err := fmt.Errorf("lookup: %w", kind)
var target *errors.Kind
require.True(t, errors.As(err, &target))
require.Same(t, kind, target)
got, ok := errors.AsType[*errors.Kind](err)
require.True(t, ok)
require.Same(t, kind, got)
}