-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
errors_test.go
119 lines (103 loc) · 3.44 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
package fuego
import (
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/require"
)
type myError struct {
status int
}
var _ ErrorWithStatus = myError{}
func (e myError) Error() string { return "test error" }
func (e myError) StatusCode() int { return e.status }
func TestErrorHandler(t *testing.T) {
t.Run("basic error", func(t *testing.T) {
err := errors.New("test error")
errResponse := ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.Contains(t, errResponse.Error(), "Internal Server Error")
require.Equal(t, http.StatusInternalServerError, errResponse.(HTTPError).StatusCode())
})
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.Contains(t, err.Error(), "Not Found :c")
require.Contains(t, errResponse.Error(), "Not Found")
require.Contains(t, errResponse.Error(), "404")
require.Equal(t, http.StatusNotFound, errResponse.(HTTPError).StatusCode())
})
t.Run("error with status ", func(t *testing.T) {
err := myError{
status: http.StatusNotFound,
}
errResponse := ErrorHandler(err)
require.ErrorAs(t, errResponse, &HTTPError{})
require.Contains(t, errResponse.Error(), "Not Found")
require.Contains(t, errResponse.Error(), "404")
require.Equal(t, http.StatusNotFound, 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.Contains(t, err.Error(), "Conflict")
require.Contains(t, errResponse.Error(), "Conflict")
require.Contains(t, errResponse.Error(), "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.Contains(t, err.Error(), "coucou")
require.Contains(t, errResponse.Error(), "Unauthorized")
require.Contains(t, errResponse.Error(), "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.Contains(t, err.Error(), "Forbidden")
require.Contains(t, errResponse.Error(), "Forbidden")
require.Contains(t, errResponse.Error(), "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.Contains(t, err.Error(), "Custom Title")
})
t.Run("title from status", func(t *testing.T) {
err := HTTPError{Status: http.StatusNotFound}
require.Contains(t, err.Error(), "Not Found")
})
t.Run("default title", func(t *testing.T) {
err := HTTPError{}
require.Contains(t, err.Error(), "Internal Server Error")
})
})
}
func TestHTTPError_Unwrap(t *testing.T) {
err := myError{status: 999}
errResponse := HTTPError{
Err: err,
}
var unwrapped myError
require.True(t, errors.As(errResponse.Unwrap(), &unwrapped))
require.Equal(t, 999, unwrapped.status)
}