Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-standard, rich error types #325

Merged
merged 4 commits into from
Feb 21, 2017
Merged

Support non-standard, rich error types #325

merged 4 commits into from
Feb 21, 2017

Conversation

akshayjshah
Copy link
Contributor

@akshayjshah akshayjshah commented Feb 19, 2017

Users who take the time (and performance hit) of using Dave Cheney's errors
package should get rich, annotated errors and stacktraces in their logs.
However, that package is only one of a number of similar packages and hasn't yet
committed to a stable API, so we don't want to take a direct dependency on it.

To support these sorts of packages, serialize errors that implement
fmt.Formatter differently from those that don't. This is necessarily slower than
serializing standard errors, but users of these packages have already committed
to a more expensive error-handling path.

@sectioneight, this should make UberFX's logs nicer.

Fixes #303.

@mention-bot
Copy link

@akshayjshah, thanks for your PR! By analyzing the history of the files in this pull request, we identified @imkira, @peter-edge and @osamingo to be potential reviewers.

@bufdev
Copy link
Contributor

bufdev commented Feb 20, 2017

LGTM

Copy link
Collaborator

@prashantv prashantv left a comment

Choose a reason for hiding this comment

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

While this is a dependency free way to use pkg/errors, I'm not sure we should do this since the error and stacktrace are under a single key, which will make it harder to filter on a single error when using something like ELK.

If anything, I think the error key should always be just the error message, and we might have a second field added if the type implements formatter, and possibly only if the formatted error message is different.

@akshayjshah
Copy link
Contributor Author

Certainly don't mind doing it that way. I'll update.

zapcore/field.go Outdated
val := f.Interface.(error)
if fancy, ok := val.(fmt.Formatter); ok {
// This is a rich error type, like those produced by
// github.com/pkg/errors.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggest verifying that the verbose error is actually different from the error message before adding.

It won't cost much extra (since the string length check will probably fail first)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done - forgot that you suggested that in the first round.

Akshay Shah added 4 commits February 20, 2017 14:04
This necessitates updating all dependencies.
Users who take the time (and performance hit) of using Dave Cheney's errors
package should get rich, annotated errors and stacktraces in their logs.
However, that package is only one of a number of similar packages hasn't yet
committed to a stable API, so we don't want to take a direct dependency on it.

To support these sorts of packages, serialize errors that implement
`fmt.Formatter` differently from those that don't.
Copy link
Collaborator

@prashantv prashantv left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants