@@ -13,13 +13,13 @@ import (
1313
1414// HTTPError is an error with a message and an HTTP status code.
1515type 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
8585func (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
9193func (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
9798func 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 {
0 commit comments