-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
errors_test.go
148 lines (129 loc) · 4.17 KB
/
errors_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
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
package fuego
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
type myError struct {
status int
err HTTPError
detail string
}
var (
_ ErrorWithStatus = myError{}
_ ErrorWithDetail = 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 HTTPError(e.err) }
func TestErrorHandler(t *testing.T) {
t.Run("basic", func(t *testing.T) {
err := errors.New("test error")
errResponse := ErrorHandler(err)
require.ErrorContains(t, errResponse, "test error")
})
t.Run("not found error", func(t *testing.T) {
err := NotFoundError{
Err: errors.New("Not Found :c"),
}
errResponse := ErrorHandler(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("not duplicate HTTPError", func(t *testing.T) {
err := HTTPError{
Err: errors.New("HTTPError"),
}
errResponse := ErrorHandler(err)
var httpError HTTPError
require.ErrorAs(t, errResponse, &httpError)
require.NotErrorAs(t, httpError.Err, &HTTPError{})
require.ErrorContains(t, err, "Internal Server Error")
})
t.Run("error with status", func(t *testing.T) {
err := myError{
status: http.StatusNotFound,
}
errResponse := ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, errResponse, "Not Found")
require.ErrorContains(t, errResponse, "404")
require.Equal(t, http.StatusNotFound, errResponse.(HTTPError).StatusCode())
})
t.Run("error with detail", func(t *testing.T) {
err := myError{
detail: "my detail",
}
errResponse := ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.Contains(t, errResponse.Error(), "Internal Server Error")
require.Contains(t, errResponse.Error(), "500")
require.Contains(t, errResponse.Error(), "my detail")
require.Equal(t, http.StatusInternalServerError, errResponse.(HTTPError).StatusCode())
})
t.Run("conflict error", func(t *testing.T) {
err := ConflictError{
Err: errors.New("Conflict"),
}
errResponse := ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, err, "Conflict")
require.ErrorContains(t, errResponse, "Conflict")
require.ErrorContains(t, errResponse, "409")
require.Equal(t, http.StatusConflict, errResponse.(HTTPError).StatusCode())
})
t.Run("unauthorized error", func(t *testing.T) {
err := UnauthorizedError{
Err: errors.New("coucou"),
}
errResponse := ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, err, "coucou")
require.ErrorContains(t, errResponse, "Unauthorized")
require.ErrorContains(t, errResponse, "401")
require.Equal(t, http.StatusUnauthorized, errResponse.(HTTPError).StatusCode())
})
t.Run("forbidden error", func(t *testing.T) {
err := ForbiddenError{
Err: errors.New("Forbidden"),
}
errResponse := ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.ErrorContains(t, err, "Forbidden")
require.ErrorContains(t, errResponse, "Forbidden")
require.ErrorContains(t, errResponse, "403")
require.Equal(t, http.StatusForbidden, 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)
}