-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
89 lines (70 loc) · 3.29 KB
/
Copy patherrors_test.go
File metadata and controls
89 lines (70 loc) · 3.29 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
package dhttp
import (
"context"
"errors"
"fmt"
"net/http/httptest"
"strings"
"testing"
pkgErrors "github.com/pkg/errors"
"github.com/streamingfast/logging"
tracing "github.com/streamingfast/sf-tracing"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
func TestWriteError(t *testing.T) {
const testTraceID = "00000000000000000000000000000001"
testContext := func() context.Context {
return tracing.WithTraceID(context.Background(), tracing.NewFixedTraceID(testTraceID))
}
errInvalidJSON := func(id string) *ErrorResponse { return InvalidJSONError(testContext(), errors.New(id)) }
errUnexpected := func(cause error) *ErrorResponse { return UnexpectedError(testContext(), cause) }
tests := []struct {
name string
err error
expectedStatusCode int
expectedBody string
}{
{"plain standard error", errors.New("test"), 500, `
{"code":"unexpected_error","trace_id":"%s","message":"An unexpected error occurred."}
`},
{"plain error response", errInvalidJSON("plain error response"), 400, `
{"code":"invalid_json_error","trace_id":"%s","details":{"errors":{"source":"plain error response"}},"message":"The request is not a valid json."}
`},
{"wrapped error, no cause", pkgErrors.Wrap(nil, "test"), 500, `
{"code":"unexpected_error","trace_id":"%s","message":"An unexpected error occurred."}
`},
{"wrapped error, cause standard error", pkgErrors.Wrap(errors.New("wrapped"), "test"), 500, `
{"code":"unexpected_error","trace_id":"%s","message":"An unexpected error occurred."}
`},
{"wrapped error, cause error response", pkgErrors.Wrap(errInvalidJSON("wrapped response"), "test"), 400, `
{"code":"invalid_json_error","trace_id":"%s","details":{"errors":{"source":"wrapped response"}},"message":"The request is not a valid json."}
`},
{"wrapped error, nested cause error response", pkgErrors.Wrap(pkgErrors.Wrap(errInvalidJSON("wrapped again"), "source"), "nested"), 400, `
{"code":"invalid_json_error","trace_id":"%s","details":{"errors":{"source":"wrapped again"}},"message":"The request is not a valid json."}
`},
{"wrapped error, unexpected with response clause", errUnexpected(errInvalidJSON("json")), 500, `
{"code":"unexpected_error","trace_id":"%s","message":"An unexpected error occurred."}
`},
{"wrapped error, unexpected with wrapped response clause", errUnexpected(pkgErrors.Wrap(errInvalidJSON("json"), "nested1")), 500, `
{"code":"unexpected_error","trace_id":"%s","message":"An unexpected error occurred."}
`},
{"wrapped error, wrapped with unexpected with wrapped response clause", pkgErrors.Wrap(errUnexpected(pkgErrors.Wrap(errInvalidJSON("json"), "nested1")), "deep"), 500, `
{"code":"unexpected_error","trace_id":"%s","message":"An unexpected error occurred."}
`},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := testContext()
recorder := httptest.NewRecorder()
logger := zap.NewExample()
WriteErrorResponse(logging.WithLogger(ctx, logger), recorder, "prefix", test.err)
assert.Equal(t, test.expectedStatusCode, recorder.Code)
expectedBody := strings.TrimSpace(test.expectedBody)
if strings.Count(expectedBody, "%s") >= 1 {
expectedBody = fmt.Sprintf(test.expectedBody, testTraceID)
}
assert.JSONEq(t, expectedBody, recorder.Body.String())
})
}
}