go get github.com/rprtr258/xerr
err := xerr.New(
xerr.Message("user not found"),
xerr.Errors{sql.NoRows},
xerr.Fields{"trace_id": "abcabcabcabc"},
xerr.Fields{
"user_id": 1234,
"user_page": "/posts",
},
)
Trace id can be added as option from context in following way:
func WithTrace(ctx) xerr.Option {
return xerr.Fields{"trace_id": getTraceIDFromCtx(ctx)}
}
See post on motivation for this library.
Multiple libraries are around in go to help handling errors. But each one of them tackles only one task, e.g. adding caller metadata, adding stack metadata, formatting error messages, wrapping error, wrapping multiple errors, structured errors.
None of them is widely used to support all listed features and/or so is not supported actively. So I wrote all utils funcs I want to use while doing error handling making this lib.