@@ -8,16 +8,18 @@ import (
88 "github.com/bugsnag/bugsnag-go/v2"
99 "github.com/netlify/netlify-commons/metriks"
1010 "github.com/netlify/netlify-commons/tracing"
11+ "github.com/sirupsen/logrus"
1112)
1213
1314// HTTPError is an error with a message and an HTTP status code.
1415type HTTPError struct {
15- Code int `json:"code"`
16- Message string `json:"msg"`
17- JSON interface {} `json:"json,omitempty"`
18- InternalError error `json:"-"`
19- InternalMessage string `json:"-"`
20- ErrorID string `json:"error_id,omitempty"`
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:"-"`
2123}
2224
2325// BadRequestError creates a 400 HTTP error
@@ -79,10 +81,25 @@ func (e *HTTPError) WithInternalMessage(fmtString string, args ...interface{}) *
7981 return e
8082}
8183
84+ // WithFields will add fields to an error message
85+ func (e * HTTPError ) WithFields (fields logrus.Fields ) * HTTPError {
86+ for key , value := range fields {
87+ e .Fields [key ] = value
88+ }
89+ return e
90+ }
91+
92+ // WithFields will add fields to an error message
93+ func (e * HTTPError ) WithField (key string , value interface {}) * HTTPError {
94+ e .Fields [key ] = value
95+ return e
96+ }
97+
8298func httpError (code int , fmtString string , args ... interface {}) * HTTPError {
8399 return & HTTPError {
84100 Code : code ,
85101 Message : fmt .Sprintf (fmtString , args ... ),
102+ Fields : make (logrus.Fields ),
86103 }
87104}
88105
@@ -102,6 +119,8 @@ func HandleError(err error, w http.ResponseWriter, r *http.Request) {
102119
103120 switch e := err .(type ) {
104121 case * HTTPError :
122+ log = log .WithFields (e .Fields )
123+
105124 e .ErrorID = errorID
106125 if e .Code >= http .StatusInternalServerError {
107126 notifyBugsnag = true
0 commit comments