pkg / errors

Simple error handling primitives
https://godoc.org/github.com/pkg/errors
BSD 2-Clause "Simplified" License
8.18k stars 691 forks source link

Proposal: json.Marshaler support for stack traces #196

Closed flimzy closed 5 years ago

flimzy commented 5 years ago

This may be a long shot, but I wonder if it would be worth while to add json.Marshaler support for stack traces, to make it easier to pass stack traces to, for instance, logrus.

I'm thinking that implementing the json.Marshaler interface on the errors.Frame type might be the best approach. This would make it possible to log a stack trace as simply as:

if stackErr, ok := err.(stackTracer); ok {
    log.WithField("stacktrace", stackErr.StackTrace())
}

and the JSON output ought to be a nice array of stack trace frames.

This would of course raise the question of precisely how to format these frames. I'm using my own implementation now which is essentially the %+v format verb's output, without newlines and tabs. Whatever format is chosen, consumers would be free to write their own marshaler and wrap the type, if necessary for special use cases.

Are there any drawbacks to this? Other thoughts or considerations?

davecheney commented 5 years ago

This seams like a reasonable addition