-
Notifications
You must be signed in to change notification settings - Fork 2
/
error_test.go
282 lines (255 loc) · 8.22 KB
/
error_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
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
package utils_test
import (
"encoding/json"
"errors"
"net/http"
"testing"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/dollarsignteam/go-utils"
)
func TestCommonError_Error(t *testing.T) {
errMessage := "Something went wrong"
commonErr := utils.CommonError{
StatusCode: 500,
ErrorCode: "ERROR",
}
result := commonErr.Error()
assert.Equal(t, errMessage, result)
}
func TestCommonError_MarshalJSON(t *testing.T) {
errMessage := "Internal error"
err := errors.New(errMessage)
commonErr := utils.CommonError{
StatusCode: 500,
ErrorCode: "ERROR",
ErrorInstance: err,
}
result, err := json.Marshal(commonErr)
assert.NoError(t, err)
expected := `{"statusCode":500,"errorCode":"ERROR","errorMessage":"Internal error"}`
assert.JSONEq(t, expected, string(result))
}
func TestCommonError_UnmarshalJSON(t *testing.T) {
jsonStr := `{"statusCode":500,"errorCode":"ERROR","errorMessage":"Internal error"}`
var commonErr utils.CommonError
err := json.Unmarshal([]byte(jsonStr), &commonErr)
assert.NoError(t, err)
assert.Equal(t, 500, commonErr.StatusCode)
assert.Equal(t, "ERROR", commonErr.ErrorCode)
assert.Equal(t, "Internal error", commonErr.Error())
}
func TestCommonError_UnmarshalJSON_InvalidError(t *testing.T) {
jsonStr := `[]`
var commonErr utils.CommonError
err := json.Unmarshal([]byte(jsonStr), &commonErr)
assert.Error(t, err)
assert.Equal(t, "Something went wrong", commonErr.Error())
}
func TestValidationError_Error(t *testing.T) {
errMessage := "Validation failed"
validationErr := utils.ValidationError{
ErrorMessage: errMessage,
}
result := validationErr.Error()
assert.Equal(t, errMessage, result)
}
func TestValidationErrorDetail(t *testing.T) {
detail := utils.ValidationErrorDetail{
Field: "id",
Tag: "required",
Message: "Key: 'Member.id' Error:Field validation for 'id' failed on the 'required' tag'",
}
field := detail.Field
tag := detail.Tag
message := detail.Message
assert.Equal(t, "id", field)
assert.Equal(t, "required", tag)
assert.Equal(t, "Key: 'Member.id' Error:Field validation for 'id' failed on the 'required' tag'", message)
}
func TestNewCommonErrorSomethingWentWrong(t *testing.T) {
err := errors.New("test error")
commonErr := utils.NewCommonErrorSomethingWentWrong(err)
assert.Equal(t, http.StatusInternalServerError, commonErr.StatusCode)
assert.Equal(t, utils.ErrCodeSomethingWentWrong, commonErr.ErrorCode)
assert.Equal(t, err, commonErr.ErrorInstance)
assert.Equal(t, err.Error(), commonErr.Error())
}
func TestNewCommonErrorBadRequest(t *testing.T) {
err := errors.New("test error")
commonErr := utils.NewCommonErrorBadRequest(err)
assert.Equal(t, http.StatusBadRequest, commonErr.StatusCode)
assert.Equal(t, utils.ErrCodeBadRequest, commonErr.ErrorCode)
assert.Equal(t, err, commonErr.ErrorInstance)
assert.Equal(t, err.Error(), commonErr.Error())
}
func TestParseCommonError(t *testing.T) {
err := errors.New("test error")
commonErr := utils.CommonError{
StatusCode: http.StatusBadRequest,
ErrorCode: "BadRequest",
ErrorInstance: err,
}
assert.Equal(t, commonErr, utils.ParseCommonError(commonErr))
newCommonErr := utils.ParseCommonError(err)
assert.Equal(t, http.StatusInternalServerError, newCommonErr.StatusCode)
assert.Equal(t, utils.ErrCodeSomethingWentWrong, newCommonErr.ErrorCode)
assert.Equal(t, err, newCommonErr.ErrorInstance)
assert.Equal(t, err.Error(), newCommonErr.Error())
}
func TestParseValidationError(t *testing.T) {
t.Run("ValidationError", func(t *testing.T) {
err := utils.ValidationError{
ErrorMessage: "test error message",
}
result := utils.ParseValidationError(err)
assert.Equal(t, err, result)
})
t.Run("ValidationErrors", func(t *testing.T) {
data := Data{Balance: "foo"}
err := utils.Validate.Struct(data)
result := utils.ParseValidationError(err)
expected := utils.ValidationError{
ErrorMessage: "Validation failed for 'Balance'",
Details: []utils.ValidationErrorDetail{
{
Field: "Balance",
Tag: "number_string",
Message: "Key: 'Data.Balance', Error: Validation for 'Balance' failed on the 'number_string' tag",
},
},
Errors: err.(validator.ValidationErrors),
}
assert.Equal(t, expected, result)
})
t.Run("UnknownError", func(t *testing.T) {
errMessage := "test error message"
err := errors.New(errMessage)
result := utils.ParseValidationError(err)
assert.EqualError(t, result, errMessage)
})
}
func TestIsCommonError(t *testing.T) {
tests := []struct {
Input error
Expected bool
}{
{
Input: utils.CommonError{
StatusCode: 500,
ErrorCode: "ERROR",
},
Expected: true,
},
{
Input: errors.New("regular error"),
Expected: false,
},
}
for _, test := range tests {
result := utils.IsCommonError(test.Input)
assert.Equal(t, test.Expected, result)
}
}
type UnknownError struct {
utils.CommonError
}
func TestIsValidationError(t *testing.T) {
tests := []struct {
Input error
Expected bool
}{
{
Input: utils.ValidationError{
ErrorMessage: "validation error",
},
Expected: true,
},
{
Input: errors.New("regular error"),
Expected: false,
},
{
Input: UnknownError{},
Expected: false,
},
}
for _, test := range tests {
result := utils.IsValidationError(test.Input)
assert.Equal(t, test.Expected, result)
}
}
func TestParseErrorResponse(t *testing.T) {
t.Run("HTTPErrorBadRequest", func(t *testing.T) {
err := echo.ErrBadRequest
resp := utils.ParseErrorResponse(err)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
assert.Equal(t, utils.ErrCodeBadRequest, resp.ErrorCode)
assert.Equal(t, utils.ErrMessageBadRequest, resp.ErrorMessage)
})
t.Run("HTTPErrorUnauthorized", func(t *testing.T) {
err := echo.ErrUnauthorized
resp := utils.ParseErrorResponse(err)
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
assert.Equal(t, utils.ErrCodeUnauthorized, resp.ErrorCode)
assert.Equal(t, utils.ErrMessageUnauthorized, resp.ErrorMessage)
})
t.Run("HTTPErrorInternalError", func(t *testing.T) {
err := &echo.HTTPError{
Code: http.StatusInternalServerError,
Message: "Oops, something went wrong!",
Internal: errors.New("internal error"),
}
expected := "Oops, something went wrong!, internal error"
resp := utils.ParseErrorResponse(err)
assert.Equal(t, http.StatusInternalServerError, resp.StatusCode)
assert.Equal(t, utils.ErrCodeSomethingWentWrong, resp.ErrorCode)
assert.Equal(t, expected, resp.ErrorMessage)
})
t.Run("CommonErrorWithValidationError", func(t *testing.T) {
data := Data{Balance: "foo"}
errValidation := utils.ValidateStruct(data)
errMessage := "Validation failed for 'Balance'"
err := utils.CommonError{
StatusCode: http.StatusBadRequest,
ErrorCode: utils.ErrCodeBadRequest,
ErrorInstance: errValidation,
}
resp := utils.ParseErrorResponse(err)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
assert.Equal(t, utils.ErrCodeBadRequest, resp.ErrorCode)
assert.Equal(t, errMessage, resp.ErrorMessage)
assert.Equal(t, errValidation.(utils.ValidationError).Details, resp.ErrorValidation)
})
t.Run("CommonErrorWithValidationErrors", func(t *testing.T) {
data := Data{Balance: "foo"}
errValidation := utils.Validate.Struct(data)
errMessage := "Validation failed for 'Balance'"
err := utils.CommonError{
StatusCode: http.StatusBadRequest,
ErrorCode: utils.ErrCodeBadRequest,
ErrorInstance: errValidation,
}
resp := utils.ParseErrorResponse(err)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
assert.Equal(t, utils.ErrCodeBadRequest, resp.ErrorCode)
assert.Equal(t, errMessage, resp.ErrorMessage)
})
t.Run("ValidationErrorAndValidationErrors", func(t *testing.T) {
data := Data{Balance: "foo"}
errMessage := "Validation failed for 'Balance'"
errValidation := utils.Validate.Struct(data)
resp := utils.ParseErrorResponse(errValidation)
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
assert.Equal(t, utils.ErrCodeBadRequest, resp.ErrorCode)
assert.Equal(t, errMessage, resp.ErrorMessage)
})
}
func BenchmarkParseValidationError(b *testing.B) {
data := Data{Balance: "foo"}
err := utils.Validate.Struct(data)
for n := 0; n < b.N; n++ {
utils.ParseValidationError(err)
}
}