This library aims to be used as a drop-in replacement to
github.com/pkg/errors and Go's standard errors package. It also
provides network portability of error objects, in ways suitable for
distributed systems with mixed-version software compatibility.
It also provides native and comprehensive support for PII-free details and an opt-in Sentry.io reporting mechanism that automatically formats error details and strips them of PII.
See also the design RFC.
Table of contents:
| Feature | Go's <1.13 errors |
github.com/pkg/errors |
Go 1.13 errors/xerrors |
cockroachdb/errors |
|---|---|---|---|---|
error constructors (New, Errorf etc) |
✔ | ✔ | ✔ | ✔ |
error causes (Cause / Unwrap) |
✔ | ✔ | ✔ | |
cause barriers (Opaque / Handled) |
✔ | ✔ | ||
errors.As(), errors.Is() |
✔ | ✔ | ||
automatic error wrap when format ends with : %w |
✔ | ✔ | ||
| standard wrappers with efficient stack trace capture | ✔ | ✔ | ||
| transparent protobuf encode/decode with forward compatibility | ✔ | |||
errors.Is() recognizes errors across the network |
✔ | |||
| comprehensive support for PII-free reportable strings | ✔ | |||
support for both Cause() and Unwrap() go#31778 |
✔ | |||
| standard error reports to Sentry.io | ✔ | |||
| wrappers to denote assertion failures | ✔ | |||
| wrappers with issue tracker references | ✔ | |||
| wrappers for user-facing hints and details | ✔ | |||
| wrappers to attach secondary causes | ✔ | |||
wrappers to attach logtags details from context.Context |
✔ | |||
errors.FormatError(), Formatter, Printer |
(under construction) | ✔ | ||
errors.SafeFormatError(), SafeFormatter |
✔ | |||
wrapper-aware IsPermission(), IsTimeout(), IsExist(), IsNotExist() |
✔ |
"Forward compatibility" above refers to the ability of this library to recognize and properly handle network communication of error types it does not know about, for example when a more recent version of a software package sends a new error object to another system running an older version of the package.
errors.New(), etc as usual, but also see the other error leaf constructors below.errors.Wrap() as usual, but also see the other wrappers below.errors.Is() as usual.
Unique in this library: this works even if the error has traversed the network!
Also, errors.IsAny() to recognize two or more reference errors.os.IsPermission(), os.IsTimeout(), os.IsExist() and os.IsNotExist() by their analog in sub-package oserror so
that they can peek through layers of wrapping.errors.UnwrapOnce() / errors.UnwrapAll() (note: errors.Cause() and errors.Unwrap() also provided for compatibility with other error packages).errors.EncodeError() / errors.DecodeError().errors.GetSafeDetails().errors.GetAllHints()/errors.GetAllDetails() or errors.FlattenHints()/errors.FlattenDetails().errors.BuildSentryReport() / errors.ReportError().error and errors.Wrapper interfaces as usual.errors.Register{Leaf,Wrapper}{Encoder,Decoder}() in a init() function in your package.Format() that redirects to errors.FormatError().| Error detail | Error() and format %s/%q/%v |
format %+v |
GetSafeDetails() |
Sentry report via ReportError() |
|---|---|---|---|---|
main message, eg New() |
visible | visible | yes (CHANGED IN v1.6) | full (CHANGED IN v1.6) |
wrap prefix, eg WithMessage() |
visible (as prefix) | visible | yes (CHANGED IN v1.6) | full (CHANGED IN v1.6) |
stack trace, eg WithStack() |
not visible | simplified | yes | full |
hint , eg WithHint() |
not visible | visible | no | type only |
detail, eg WithDetail() |
not visible | visible | no | type only |
assertion failure annotation, eg WithAssertionFailure() |
not visible | visible | no | type only |
issue links, eg WithIssueLink(), UnimplementedError() |
not visible | visible | yes | full |
safe details, eg WithSafeDetails() |
not visible | visible | yes | full |
telemetry keys, eg. WithTelemetryKey() |
not visible | visible | yes | full |
secondary errors, eg. WithSecondaryError(), CombineErrors() |
not visible | visible | redacted, recursively | redacted, recursively |
barrier origins, eg. Handled() |
not visible | visible | redacted, recursively | redacted, recursively |
error domain, eg. WithDomain() |
not visible | visible | yes | full |
context tags, eg. WithContextTags() |
not visible | visible | keys visible, values redacted | keys visible, values redacted |
An error leaf is an object that implements the error interface,
but does not refer to another error via a Unwrap() or Cause()
method.
New(string) error, Newf(string, ...interface{}) error, Errorf(string, ...interface{}) error: leaf errors with messageError(), regular Go formatting. Details in Sentry report.see also: Section Error composition below. errors.NewWithDepth() variants to customize at which call depth the stack trace is captured.
AssertionFailedf(string, ...interface{}) error, NewAssertionFailureWithWrappedErrf(error, string, ...interface{}) error: signals an assertion failure / programming error.
IsAssertionFailure()/HasAssertionFailure(), format with %+v, Safe details included in Sentry reports.see also: Section Error composition below. errors.AssertionFailedWithDepthf() variant to customize at which call depth the stack trace is captured.
Handled(error) error, Opaque(error) error, HandledWithMessage(error, string) error: captures an error cause but make it invisible to Unwrap() / Is().
...WithMessage() variant is used.how to access the detail: format with %+v, redacted details reported in Sentry reports.
UnimplementedError(IssueLink, string) error: captures a message string and a URL reference to an external resource to denote a feature that was not yet implemented.
errors.GetAllHints(), errors.FlattenHints(), format with %+v, URL and detail included in Sentry report (not the message).errors.WithIssueLink() below for errors that are not specifically about unimplemented features.An error wrapper is an object that implements the error interface,
and also refers to another error via an Unwrap() (preferred) and/or
Cause() method.
All wrapper constructors can be applied safely to a nil error:
they behave as no-ops in this case:
// The following:
// if err := foo(); err != nil {
// return errors.Wrap(err, "foo")
// }
// return nil
//
// is not needed. Instead, you can use this:
return errors.Wrap(foo(), "foo")
Wrap(error, string) error, Wrapf(error, string, ...interface{}) error:WithMessage(), WithStack(), WithSafeDetails().$ claude mcp add errors \
-- python -m otcore.mcp_server <graph>