Documentation
¶
Index ¶
- Variables
- func As(err error, target any) bool
- func Combine(errors ...error) error
- func Has(err error, cause error) booldeprecated
- func Is(err error, target error) bool
- func Message(err error) string
- func New(opts ...Option) error
- func Newf(format string, args ...any) error
- func Wrap(err error, opts ...Option) error
- func Wrapf(err error, format string, args ...any) error
- type Category
- type Error
- type Option
Constants ¶
This section is empty.
Variables ¶
var ( Cancaled = NewCategory("Cancelled", 499) // non-standard status code for cancellation BadRequest = NewCategory("BadRequest", http.StatusBadRequest) InvalidArgument = NewCategory("InvalidArgument", http.StatusBadRequest) Forbidden = NewCategory("Forbidden", http.StatusForbidden) PermissionDenied = NewCategory("PermissionDenied", http.StatusForbidden) NotFound = NewCategory("NotFound", http.StatusNotFound) DeadlineExceeded = NewCategory("DeadlineExceeded", http.StatusRequestTimeout) Conflict = NewCategory("Conflict", http.StatusConflict) AlreadyExist = NewCategory("AlreadyExist", http.StatusConflict) TooManyRequests = NewCategory("TooManyRequests", http.StatusTooManyRequests) Internal = NewCategory("Internal", http.StatusInternalServerError) NotImplemented = NewCategory("NotImplemented", http.StatusNotImplemented) ResourceExhausted = NewCategory("ResourceExhausted", http.StatusServiceUnavailable) DBFailed = NewCategory("DBFailed", http.StatusInternalServerError) )
Functions ¶
func As ¶ added in v1.1.0
As finds the first error in err's tree whose concrete type is assignable to the value pointed to by target, sets target to that error, and reports whether one was found. Use it to recover a concrete error type and read its fields.
As adds nothing to the standard library's errors.As and exists only so that callers of this package need not also import errors under an alias, this package having taken the name.
As has nothing to do with categories. Unlike Is, it does not special-case a Category target, because a category is stored on an error rather than being an element of its tree. For a Category c, As(err, &c) reports only whether a Category value happens to sit in the chain: true for a bare NotFound.New(), false for NotFound.Newf("gone"), whose category is nonetheless NotFound. To test a category use Is; to read one use Error.Category.
func Combine ¶
Combine returns an error holding every non-nil error passed to it, or nil if there are none. The result implements Unwrap() []error, so errors.Is and errors.As examine each combined error in turn. Note that a combined error is a tree rather than a chain, and so carries no single category.
func Has
deprecated
Has reports whether cause exists anywhere in err's tree.
Deprecated: use Is, which performs the same search. Has is retained for compatibility and calls the standard library's errors.Is directly. Unlike Is, it gives a Category target no special meaning, matching one only when the category value itself appears in the tree.
func Is ¶
Is reports whether err matches target. Which search it performs depends on the dynamic type of target, because the two are not variations of one idea.
If target is a Category, Is reports whether err resolves to that category. An error carries exactly one category, decided by the outermost error in the chain that carries one; see Error.Category. Is therefore resolves err to a single category and compares the two by identity. It does not search err's tree, and it never consults the standard library: a category is stored on an error rather than wrapped by it, so errors.Is cannot find one. A category held by an inner error is shadowed by one held further out, and categories from separate NewCategory calls are never equal, even given the same name.
Otherwise Is delegates to the standard library's errors.Is, which matches target against every error in err's tree. Use this form for sentinel errors.
func Message ¶
Message returns err.Error(), or the empty string if err is nil. For errors this package produced, that is every message in the chain, joined by ": ". To read only the outermost message, use Error.Message.
func New ¶
New returns a new error configured by opts, capturing a stack trace at the call site. It never returns nil.
Always give an error a message. With no options, or with WithMessage(""), the error carries only its stack trace: it is non-nil, so callers take their error branch, but its Error method returns the empty string, so whatever logs it records nothing. Such an error reports that something failed while withholding what.
func Newf ¶
Newf returns a new error with a formatted message, capturing a stack trace at the call site. It is New with a single WithMessage option.
Never call Newf(""). The result is a non-nil error whose Error method returns the empty string: callers enter their error branch and log nothing. It also corrupts Combine, whose output for Combine(Newf(""), Newf("real")) is "; real". Give every error a message.
func Wrap ¶
Wrap returns err wrapped with opts, or nil if err is nil, so it is safe to call on a value that may not be an error.
Given no options and an err this package already produced, Wrap returns err unchanged. Otherwise it returns a new error whose cause is err, capturing a stack trace only when err did not already carry one.
func Wrapf ¶
Wrapf returns err wrapped with a formatted message, or nil if err is nil.
Never call Wrapf(err, ""). Unlike Wrap, Wrapf always applies an option, so an empty format does not yield err: it appends a message-less layer to the chain, which lengthens Chain and allocates while adding nothing a reader can see. When there is no context to add, call Wrap(err), which returns err untouched.
Types ¶
type Category ¶
type Category interface {
Error() string
StatusCode() int
New(opts ...Option) error
Newf(format string, args ...any) error
Wrap(err error, opts ...Option) error
Wrapf(err error, format string, args ...any) error
}
func LookupCategory ¶ added in v1.0.3
LookupCategory returns the category registered under name, or nil if no category has been registered under it.
func NewCategory ¶
NewCategory returns a new Category with the given name and HTTP status code, and registers it under that name for LookupCategory.
Categories compare by identity, so two calls with the same name and status code return categories that are not equal. Registering a name that is already taken replaces the earlier entry in the registry, but errors already carrying the earlier category keep it.