-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandlers_test.go
174 lines (157 loc) · 4.69 KB
/
handlers_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
package g8_test
import (
"bytes"
"errors"
"fmt"
"net/http"
"testing"
adapter "github.com/jfallis/lambda-proxy-http-adapter"
"github.com/rotisserie/eris"
"github.com/rs/zerolog"
"github.com/steinfletcher/apitest"
"github.com/stretchr/testify/assert"
"github.com/JSainsburyPLC/g8"
)
func TestError_Error(t *testing.T) {
err := g8.Err{
Status: http.StatusBadRequest,
Code: "INVALID_QUERY_PARAM",
Detail: "Invalid query param",
}
if err.Error() != "Code: INVALID_QUERY_PARAM; Status: 400; Detail: Invalid query param" {
t.Fatalf("unexpected error: '%s'", err.Error())
}
}
func TestAPIGatewayProxyHandler_UnhandledErrorResponseWithStackTrace(t *testing.T) {
h := func(c *g8.APIGatewayProxyContext) error {
return eris.Wrap(errors.New("external error"), "additional context")
}
logBuf := &bytes.Buffer{}
lh := g8.APIGatewayProxyHandler(h, g8.HandlerConfig{
Logger: zerolog.New(logBuf),
})
apitest.New().
Handler(adapter.GetHTTPHandlerWithContext(lh, "/", nil, nil)).
Get("/").
Expect(t).
Status(http.StatusInternalServerError).
Body(`{
"code": "INTERNAL_SERVER_ERROR",
"detail": "Internal server error"
}`).
HeaderPresent("Correlation-Id").
End()
assert.Equal(t, "Unhandled error", jsonPath("$.message", logBuf.Bytes()))
assert.Equal(t, "additional context", jsonPath("$.error.root.message", logBuf.Bytes()))
assert.Equal(t, "external error", jsonPath("$.error.external", logBuf.Bytes()))
assert.NotEmpty(t, jsonPath("$.error.root.stack", logBuf.Bytes()))
}
// TestAPIGatewayProxyHandler_UnhandledErrorsResponseWithStackTrace tests external errors to json
// https://github.com/rotisserie/eris/blob/a9462968dc6916f50e0c4e89c9d01faa642872f4/format_test.go#L191
func TestAPIGatewayProxyHandler_UnhandledErrorsResponseWithStackTrace(t *testing.T) {
stackRegex := "g8_test\\.TestAPIGatewayProxyHandler_UnhandledErrorsResponseWithStackTrace:.+:\\d+"
type rootErr struct {
message *string
stack []string
}
type wrapErr struct {
message string
stack string
}
type output struct {
root rootErr
wrap []wrapErr
externalMessage *string
message string
}
tests := map[string]struct {
input error
output
}{
"basic root error": {
input: eris.New("root error"),
// {"root":{"message":"root error"}}
output: output{
root: rootErr{
message: strToPtr("root error"),
stack: []string{stackRegex},
},
message: "Unhandled error",
},
},
"basic wrapped error": {
input: eris.Wrap(eris.Wrap(eris.New("root error"), "additional context"), "even more context"),
// {"root":{"message":"root error"},"wrap":[{"message":"even more context"},{"message":"additional context"}]}
output: output{
root: rootErr{
message: strToPtr("root error"),
stack: []string{stackRegex, stackRegex, stackRegex},
},
wrap: []wrapErr{
{
message: "even more context",
stack: stackRegex,
},
{
message: "additional context",
stack: stackRegex,
},
},
message: "Unhandled error",
},
},
"external error": {
input: eris.Wrap(errors.New("external error"), "additional context"),
// {"external":"external error","root":{"message":"additional context"}}
output: output{
externalMessage: strToPtr("external error"),
root: rootErr{
message: strToPtr("additional context"),
stack: []string{stackRegex},
},
message: "Unhandled error",
},
},
}
for desc, tt := range tests {
t.Run(desc, func(t *testing.T) {
h := func(c *g8.APIGatewayProxyContext) error {
return tt.input
}
logBuf := &bytes.Buffer{}
lh := g8.APIGatewayProxyHandler(h, g8.HandlerConfig{
Logger: zerolog.New(logBuf),
})
apitest.New().
Handler(adapter.GetHTTPHandlerWithContext(lh, "/", nil, nil)).
Get("/").
Expect(t).
Status(http.StatusInternalServerError).
Body(`{
"code": "INTERNAL_SERVER_ERROR",
"detail": "Internal server error"
}`).
HeaderPresent("Correlation-Id").
End()
// root
if tt.root.message != nil {
assert.Equal(t, *tt.root.message, jsonPath("$.error.root.message", logBuf.Bytes()))
}
for x, stack := range tt.root.stack {
assert.Regexp(t, stack, jsonPath(fmt.Sprintf("$.error.root.stack[%d]", x), logBuf.Bytes()))
}
// wrap
for x, stack := range tt.wrap {
assert.Equal(t, stack.message, jsonPath(fmt.Sprintf("$.error.wrap[%d].message", x), logBuf.Bytes()))
assert.Regexp(t, stack.stack, jsonPath(fmt.Sprintf("$.error.wrap[%d].stack", x), logBuf.Bytes()))
}
// external
if tt.externalMessage != nil {
assert.Equal(t, *tt.externalMessage, jsonPath("$.error.external", logBuf.Bytes()))
}
})
}
}
func strToPtr(v string) *string {
return &v
}