Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions router/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import (
"github.com/bugsnag/bugsnag-go/v2"
"github.com/netlify/netlify-commons/metriks"
"github.com/netlify/netlify-commons/tracing"
"github.com/sirupsen/logrus"
)

// HTTPError is an error with a message and an HTTP status code.
type HTTPError struct {
Code int `json:"code"`
Message string `json:"msg"`
JSON interface{} `json:"json,omitempty"`
InternalError error `json:"-"`
InternalMessage string `json:"-"`
ErrorID string `json:"error_id,omitempty"`
Code int `json:"code"`
Message string `json:"msg"`
JSON interface{} `json:"json,omitempty"`
InternalError error `json:"-"`
InternalMessage string `json:"-"`
ErrorID string `json:"error_id,omitempty"`
Fields []logrus.Fields `json:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logrus.Fields is just an alias for a hashmap. multiple entries should be merged, which is the same logrus does for multiple calls to WithFields
you could also use a *logrus.Entry which you could call WithFields on to use their merging logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some naive merging logic, and a test: b692cb1

}

// BadRequestError creates a 400 HTTP error
Expand Down Expand Up @@ -79,6 +81,19 @@ func (e *HTTPError) WithInternalMessage(fmtString string, args ...interface{}) *
return e
}

// WithFields will add fields to an error message
func (e *HTTPError) WithFields(fields logrus.Fields) *HTTPError {
e.Fields = append(e.Fields, fields)
return e
}

// WithFields will add fields to an error message
func (e *HTTPError) WithField(key string, value interface{}) *HTTPError {
return e.WithFields(logrus.Fields{
key: value,
})
}

func httpError(code int, fmtString string, args ...interface{}) *HTTPError {
return &HTTPError{
Code: code,
Expand All @@ -102,6 +117,10 @@ func HandleError(err error, w http.ResponseWriter, r *http.Request) {

switch e := err.(type) {
case *HTTPError:
for _, fields := range e.Fields {
log = log.WithFields(fields)
}

e.ErrorID = errorID
if e.Code >= http.StatusInternalServerError {
notifyBugsnag = true
Expand Down