Skip to content

Conversation

@Skn0tt
Copy link
Contributor

@Skn0tt Skn0tt commented Feb 24, 2022

This PR adds .WithFields and .WithField methods to HTTPError, which act as a facade to Logrus' methods.
They allow to easily add custom log fields to error messages.

how this helps

At the moment, when you want to add additional information to an HTTP error, you need to print two log lines:

logger.WithError(err).WithField("body", string(body)).Error("error unmarshaling events")
return router.BadRequestError("error parsing body: %s", err.Error())

With this PR, we can trim it down to one:

return router.BadRequestError("error parsing body: %s", err.Error()).WithInternalError(err).WithField("body", string(body))

(taken from https://github.com/netlify/ingesteer/pull/18#discussion_r813652764)

@Skn0tt Skn0tt requested a review from a team as a code owner February 24, 2022 08:41
@github-actions github-actions bot added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Feb 24, 2022
router/errors.go Outdated
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

@Skn0tt Skn0tt merged commit 35dff55 into main Mar 3, 2022
@Skn0tt Skn0tt deleted the error-withfields branch March 3, 2022 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature code contributing to the implementation of a feature and/or user facing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants