-
Notifications
You must be signed in to change notification settings - Fork 18
/
error_test.go
90 lines (74 loc) · 2.24 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
package rest_test
import (
"encoding/json"
"errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/swaggest/rest"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
)
func TestHTTPStatusFromCanonicalCode(t *testing.T) {
maxStatusCode := 17
for i := 0; i <= maxStatusCode; i++ {
s := status.Code(i)
assert.NotEmpty(t, rest.HTTPStatusFromCanonicalCode(s))
}
}
type errWithHTTPStatus int
func (e errWithHTTPStatus) Error() string {
return "failed very much"
}
func (e errWithHTTPStatus) HTTPStatus() int {
return int(e)
}
func TestErr(t *testing.T) {
err := usecase.Error{
StatusCode: status.InvalidArgument,
Value: errors.New("failed"),
Context: map[string]interface{}{"hello": "world"},
}
code, er := rest.Err(err)
assert.Equal(t, http.StatusBadRequest, code)
assert.Equal(t, map[string]interface{}{"hello": "world"}, er.Context)
assert.Equal(t, "invalid argument: failed", er.Error())
assert.Equal(t, "INVALID_ARGUMENT", er.StatusText)
assert.Equal(t, 0, er.AppCode)
assert.Equal(t, err, er.Unwrap())
j, jErr := json.Marshal(er)
assert.NoError(t, jErr)
assert.Equal(t,
`{"status":"INVALID_ARGUMENT","error":"invalid argument: failed","context":{"hello":"world"}}`,
string(j),
)
code, er = rest.Err(status.DataLoss)
assert.Equal(t, http.StatusInternalServerError, code)
assert.Nil(t, er.Context)
assert.Equal(t, "data loss", er.Error())
assert.Equal(t, "DATA_LOSS", er.StatusText)
assert.Equal(t, 0, er.AppCode)
assert.Equal(t, status.DataLoss, er.Unwrap())
err = usecase.Error{
AppCode: 123,
StatusCode: status.InvalidArgument,
Value: errors.New("failed"),
}
code, er = rest.Err(err)
assert.Nil(t, er.Context)
assert.Equal(t, http.StatusBadRequest, code)
assert.Equal(t, "invalid argument: failed", er.Error())
assert.Equal(t, "INVALID_ARGUMENT", er.StatusText)
assert.Equal(t, 123, er.AppCode)
assert.Equal(t, err, er.Unwrap())
code, er = rest.Err(errWithHTTPStatus(http.StatusTeapot))
assert.Nil(t, er.Context)
assert.Equal(t, http.StatusTeapot, code)
assert.Equal(t, "failed very much", er.Error())
assert.Equal(t, "", er.StatusText)
assert.Equal(t, 0, er.AppCode)
assert.Panics(t, func() {
_, er := rest.Err(nil)
assert.NoError(t, er)
})
}