@@ -8,16 +8,18 @@ import (
8
8
"github.com/bugsnag/bugsnag-go/v2"
9
9
"github.com/netlify/netlify-commons/metriks"
10
10
"github.com/netlify/netlify-commons/tracing"
11
+ "github.com/sirupsen/logrus"
11
12
)
12
13
13
14
// HTTPError is an error with a message and an HTTP status code.
14
15
type 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:"-"`
21
23
}
22
24
23
25
// BadRequestError creates a 400 HTTP error
@@ -79,10 +81,25 @@ func (e *HTTPError) WithInternalMessage(fmtString string, args ...interface{}) *
79
81
return e
80
82
}
81
83
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
+
82
98
func httpError (code int , fmtString string , args ... interface {}) * HTTPError {
83
99
return & HTTPError {
84
100
Code : code ,
85
101
Message : fmt .Sprintf (fmtString , args ... ),
102
+ Fields : make (logrus.Fields ),
86
103
}
87
104
}
88
105
@@ -102,6 +119,8 @@ func HandleError(err error, w http.ResponseWriter, r *http.Request) {
102
119
103
120
switch e := err .(type ) {
104
121
case * HTTPError :
122
+ log = log .WithFields (e .Fields )
123
+
105
124
e .ErrorID = errorID
106
125
if e .Code >= http .StatusInternalServerError {
107
126
notifyBugsnag = true
0 commit comments