forked from go-fuego/fuego
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
245 lines (212 loc) · 7.14 KB
/
Copy patherrors_test.go
File metadata and controls
245 lines (212 loc) · 7.14 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
package fuego
import (
"context"
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thejerf/slogassert"
)
type myError struct {
status int
err HTTPError
detail string
title string
}
var (
_ ErrorWithStatus = myError{}
_ ErrorWithDetail = myError{}
_ ErrorWithTitle = myError{}
)
func (e myError) Error() string { return "test error" }
func (e myError) StatusCode() int { return e.status }
func (e myError) DetailMsg() string { return e.detail }
func (e myError) Unwrap() error { return e.err }
func (e myError) ErrorTitle() string { return e.title }
func TestErrorHandler(t *testing.T) {
t.Run("basic", func(t *testing.T) {
err := errors.New("test error")
handler := slogassert.NewDefault(t)
errResponse := ErrorHandler(context.Background(), err)
require.ErrorContains(t, errResponse, "test error")
handler.AssertMessage("Error in controller")
handler.AssertEmpty()
})
t.Run("not found error", func(t *testing.T) {
err := NotFoundError{
Err: errors.New("Not Found :c"),
}
var errHTTP HTTPError
require.ErrorAs(t, ErrorHandler(context.Background(), err), &errHTTP)
require.ErrorContains(t, err, "Not Found :c")
require.ErrorContains(t, errHTTP, "Not Found")
require.ErrorContains(t, errHTTP, "404")
assert.Equal(t, http.StatusNotFound, errHTTP.StatusCode())
})
t.Run("not duplicate HTTPError", func(t *testing.T) {
err := HTTPError{
Err: errors.New("HTTPError"),
}
var errHTTP HTTPError
require.ErrorAs(t, ErrorHandler(context.Background(), err), &errHTTP)
require.NotErrorAs(t, errHTTP.Err, &HTTPError{})
require.ErrorContains(t, err, "Internal Server Error")
})
t.Run("error with status", func(t *testing.T) {
err := myError{
status: http.StatusNotFound,
}
var errHTTP HTTPError
require.ErrorAs(t, ErrorHandler(context.Background(), err), &errHTTP)
require.ErrorContains(t, errHTTP, "Not Found")
require.ErrorContains(t, errHTTP, "404")
require.Equal(t, http.StatusNotFound, errHTTP.StatusCode())
})
t.Run("error with detail", func(t *testing.T) {
err := myError{
detail: "my detail",
}
var errHTTP HTTPError
require.ErrorAs(t, ErrorHandler(context.Background(), err), &errHTTP)
require.ErrorContains(t, errHTTP, "Internal Server Error")
require.ErrorContains(t, errHTTP, "500")
require.ErrorContains(t, errHTTP, "my detail")
assert.Equal(t, "my detail", errHTTP.DetailMsg())
})
t.Run("error with title", func(t *testing.T) {
err := myError{
title: "my title",
}
var errHTTP HTTPError
require.ErrorAs(t, ErrorHandler(context.Background(), err), &errHTTP)
require.ErrorContains(t, errHTTP, "500")
assert.Equal(t, "my title", errHTTP.ErrorTitle())
})
t.Run("conflict error", func(t *testing.T) {
err := ConflictError{
Err: errors.New("Conflict"),
}
var errHTTP HTTPError
require.ErrorAs(t, ErrorHandler(context.Background(), err), &errHTTP)
require.ErrorContains(t, err, "Conflict")
require.ErrorContains(t, errHTTP, "Conflict")
require.ErrorContains(t, errHTTP, "409")
require.Equal(t, http.StatusConflict, errHTTP.StatusCode())
})
t.Run("unauthorized error", func(t *testing.T) {
err := UnauthorizedError{
Err: errors.New("coucou"),
}
var errHTTP HTTPError
require.ErrorAs(t, ErrorHandler(context.Background(), err), &errHTTP)
require.ErrorContains(t, err, "coucou")
require.ErrorContains(t, errHTTP, "Unauthorized")
require.ErrorContains(t, errHTTP, "401")
require.Equal(t, http.StatusUnauthorized, errHTTP.StatusCode())
})
t.Run("forbidden error", func(t *testing.T) {
err := ForbiddenError{
Err: errors.New("Forbidden"),
}
var errHTTP HTTPError
require.ErrorAs(t, ErrorHandler(context.Background(), err), &errHTTP)
require.ErrorContains(t, err, "Forbidden")
require.ErrorContains(t, errHTTP, "Forbidden")
require.ErrorContains(t, errHTTP, "403")
require.Equal(t, http.StatusForbidden, errHTTP.StatusCode())
})
}
func TestHandleHTTPError(t *testing.T) {
t.Run("should always be HTTPError", func(t *testing.T) {
err := errors.New("test error")
errResponse := HandleHTTPError(context.Background(), err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, errResponse, "500 Internal Server Error")
})
t.Run("not found error", func(t *testing.T) {
err := NotFoundError{
Err: errors.New("Not Found :c"),
}
errResponse := HandleHTTPError(context.Background(), err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, err, "Not Found :c")
require.ErrorContains(t, errResponse, "Not Found")
require.ErrorContains(t, errResponse, "404")
require.Equal(t, http.StatusNotFound, errResponse.(HTTPError).StatusCode())
})
t.Run("error is a reference to HTTPError", func(t *testing.T) {
err := &HTTPError{
Title: "HTTPError",
Errors: []ErrorItem{{Name: "my name"}},
Err: errors.New("my new error"),
}
errResponse := HandleHTTPError(context.Background(), err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, err, "my new error")
require.ErrorContains(t, errResponse, "HTTPError")
require.ErrorContains(t, errResponse, "500")
require.Equal(t, http.StatusInternalServerError, errResponse.(HTTPError).StatusCode())
})
}
func TestHTTPError_Error(t *testing.T) {
t.Run("title", func(t *testing.T) {
t.Run("custom title", func(t *testing.T) {
err := HTTPError{
Title: "Custom Title",
}
require.ErrorContains(t, err, "Custom Title")
})
t.Run("title from status", func(t *testing.T) {
err := HTTPError{Status: http.StatusNotFound}
require.ErrorContains(t, err, "Not Found")
})
t.Run("default title", func(t *testing.T) {
err := HTTPError{}
require.ErrorContains(t, err, "Internal Server Error")
})
})
}
func TestHTTPError_Unwrap(t *testing.T) {
err := myError{status: 999}
errResponse := HTTPError{
Err: err,
}
var unwrapped myError
require.ErrorAs(t, errResponse.Unwrap(), &unwrapped)
require.Equal(t, 999, unwrapped.status)
}
func TestUnauthorizedError(t *testing.T) {
t.Run("without error", func(t *testing.T) {
err := UnauthorizedError{Title: "Unauthorized"}
assert.EqualError(t, err, "401 Unauthorized")
})
t.Run("with error", func(t *testing.T) {
err := UnauthorizedError{Title: "Unauthorized", Err: errors.New("error message")}
assert.EqualError(t, err, "401 Unauthorized: error message")
})
t.Run("with error and detail", func(t *testing.T) {
err := UnauthorizedError{Title: "Unauthorized", Detail: "detail message", Err: errors.New("error message")}
assert.EqualError(t, err, "401 Unauthorized (detail message): error message")
})
}
func TestForbiddenError(t *testing.T) {
t.Run("without error", func(t *testing.T) {
err := ForbiddenError{Title: "Access forbidden"}
assert.EqualError(t, err, "403 Access forbidden")
})
t.Run("with error", func(t *testing.T) {
err := ForbiddenError{Title: "Access forbidden", Err: errors.New("error message")}
assert.EqualError(t, err, "403 Access forbidden: error message")
})
}
func BenchmarkHTTPError_PublicError(b *testing.B) {
err := HTTPError{
Title: "Custom Title",
Detail: "Custom Detail",
Status: http.StatusNotFound,
}
for b.Loop() {
_ = err.PublicError()
}
}