Repository navigation
Expand file tree
/
Copy pathresponse_error_test.go
More file actions
101 lines (87 loc) · 3.1 KB
/
Copy pathresponse_error_test.go
File metadata and controls
101 lines (87 loc) · 3.1 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
package api
import (
"fmt"
"net/http"
"testing"
"github.com/jgolang/api/core"
"github.com/jgolang/errors"
"github.com/jgolang/errors/codes"
)
var errorHelpers = []struct {
name string
build func(error) Error
wantMessage bool // false when the helper keeps its own message
}{
{name: "Error400", build: func(err error) Error { return Error400(err) }, wantMessage: true},
{name: "ErrorWithMsg", build: func(err error) Error { return ErrorWithMsg("custom", err) }, wantMessage: false},
{name: "Error500", build: func(err error) Error { return Error500(err) }, wantMessage: true},
{name: "Error501", build: func(err error) Error { return Error501(err) }, wantMessage: true},
}
func TestErrorHelpersKeepCodeOfWrappedErrors(t *testing.T) {
notFound := codes.New("PLN-111", "task not found")
coded := errors.NewC(notFound, "task 42 is missing")
cases := []struct {
name string
err error
}{
{name: "unwrapped", err: coded},
{name: "behind fmt.Errorf", err: fmt.Errorf("loading board: %w", coded)},
{name: "behind two wrappers", err: fmt.Errorf("handler: %w", fmt.Errorf("loading: %w", coded))},
{name: "behind a nil-code error", err: errors.WrapC(coded, nil)},
}
for _, helper := range errorHelpers {
for _, tc := range cases {
t.Run(helper.name+"/"+tc.name, func(t *testing.T) {
response := helper.build(tc.err)
if response.ResponseCode != core.ResponseCode("PLN-111") {
t.Fatalf("expected code PLN-111, got %q", response.ResponseCode)
}
if helper.wantMessage && response.Message != "task not found" {
t.Fatalf("expected message %q, got %q", "task not found", response.Message)
}
})
}
}
}
func TestErrorHelpersWithoutCodeUseDefaults(t *testing.T) {
cases := []struct {
name string
err error
}{
{name: "nil code", err: errors.WrapC(fmt.Errorf("plain failure"), nil)},
{name: "typed nil", err: fmt.Errorf("x: %w", (*errors.Error)(nil))},
{name: "plain error", err: fmt.Errorf("plain failure")},
}
for _, tc := range cases {
t.Run("Error400/"+tc.name, func(t *testing.T) {
response := Error400(tc.err)
if response.ResponseCode != "" || response.Message != "" {
t.Fatalf("expected empty code and message for Write to default, got %q %q", response.ResponseCode, response.Message)
}
})
t.Run("Error500/"+tc.name, func(t *testing.T) {
response := Error500(tc.err)
if response.ResponseCode != ResponseCodes.InternalServerEerror || response.Message != InternalServerMessage {
t.Fatalf("expected the Error500 defaults, got %q %q", response.ResponseCode, response.Message)
}
})
}
}
func TestErrorHelpersHTTPStatus(t *testing.T) {
cases := []struct {
name string
build func() Error
status int
}{
{name: "Error500", build: func() Error { return Error500() }, status: http.StatusInternalServerError},
{name: "Error501", build: func() Error { return Error501() }, status: http.StatusNotImplemented},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
response := tc.build()
if response.HTTPStatusCode != tc.status {
t.Fatalf("expected status %d, got %d", tc.status, response.HTTPStatusCode)
}
})
}
}