Skip to content

Commit b692cb1

Browse files
committed
fix: add test
1 parent fb496df commit b692cb1

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

router/errors.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import (
1313

1414
// HTTPError is an error with a message and an HTTP status code.
1515
type HTTPError struct {
16-
Code int `json:"code"`
17-
Message string `json:"msg"`
18-
JSON interface{} `json:"json,omitempty"`
19-
InternalError error `json:"-"`
20-
InternalMessage string `json:"-"`
21-
ErrorID string `json:"error_id,omitempty"`
22-
Fields []logrus.Fields `json:"-"`
16+
Code int `json:"code"`
17+
Message string `json:"msg"`
18+
JSON interface{} `json:"json,omitempty"`
19+
InternalError error `json:"-"`
20+
InternalMessage string `json:"-"`
21+
ErrorID string `json:"error_id,omitempty"`
22+
Fields logrus.Fields `json:"-"`
2323
}
2424

2525
// BadRequestError creates a 400 HTTP error
@@ -83,21 +83,23 @@ func (e *HTTPError) WithInternalMessage(fmtString string, args ...interface{}) *
8383

8484
// WithFields will add fields to an error message
8585
func (e *HTTPError) WithFields(fields logrus.Fields) *HTTPError {
86-
e.Fields = append(e.Fields, fields)
86+
for key, value := range fields {
87+
e.Fields[key] = value
88+
}
8789
return e
8890
}
8991

9092
// WithFields will add fields to an error message
9193
func (e *HTTPError) WithField(key string, value interface{}) *HTTPError {
92-
return e.WithFields(logrus.Fields{
93-
key: value,
94-
})
94+
e.Fields[key] = value
95+
return e
9596
}
9697

9798
func httpError(code int, fmtString string, args ...interface{}) *HTTPError {
9899
return &HTTPError{
99100
Code: code,
100101
Message: fmt.Sprintf(fmtString, args...),
102+
Fields: make(logrus.Fields),
101103
}
102104
}
103105

@@ -117,9 +119,7 @@ func HandleError(err error, w http.ResponseWriter, r *http.Request) {
117119

118120
switch e := err.(type) {
119121
case *HTTPError:
120-
for _, fields := range e.Fields {
121-
log = log.WithFields(fields)
122-
}
122+
log = log.WithFields(e.Fields)
123123

124124
e.ErrorID = errorID
125125
if e.Code >= http.StatusInternalServerError {

router/errors_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,36 @@ func TestHandleError_HTTPError(t *testing.T) {
122122
assert.Equal(t, "internal server error: "+httpErr.InternalMessage, loggerOutput.AllEntries()[0].Message)
123123
}
124124

125+
func TestHandleError_HttpErrorWithFields(t *testing.T) {
126+
logger, loggerOutput := test.NewNullLogger()
127+
recorder := httptest.NewRecorder()
128+
w, r, _ := tracing.NewTracer(
129+
recorder,
130+
httptest.NewRequest(http.MethodGet, "/", nil),
131+
logger,
132+
"test",
133+
"test",
134+
)
135+
136+
httpErr := httpError(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
137+
138+
httpErr.WithFields(map[string]interface{}{
139+
"a": "1",
140+
"b": "2",
141+
})
142+
143+
httpErr.WithField("c", "3")
144+
httpErr.WithField("a", "0")
145+
146+
HandleError(httpErr, w, r)
147+
148+
require.Len(t, loggerOutput.AllEntries(), 1)
149+
entry := loggerOutput.LastEntry()
150+
assert.Equal(t, entry.Data["a"], "0")
151+
assert.Equal(t, entry.Data["b"], "2")
152+
assert.Equal(t, entry.Data["c"], "3")
153+
}
154+
125155
func TestHandleError_NoLogForNormalErrors(t *testing.T) {
126156
logger, loggerOutput := test.NewNullLogger()
127157
recorder := httptest.NewRecorder()

0 commit comments

Comments
 (0)