import "github.com/juju/errors"
The juju/errors provides an easy way to annotate errors without losing the original error context.
The exported New and Errorf functions are designed to replace the
errors.New and fmt.Errorf functions respectively. The same underlying
error is there, but the package also records the location at which the error
was created.
A primary use case for this library is to add extra context any time an error is returned from a function.
if err := SomeFunc(); err != nil {
return err
}
This instead becomes:
if err := SomeFunc(); err != nil {
return errors.Trace(err)
}
which just records the file and line number of the Trace call, or
if err := SomeFunc(); err != nil {
return errors.Annotate(err, "more context")
}
which also adds an annotation to the error.
When you want to check to see if an error is of a particular type, a helper
function is normally exported by the package that returned the error, like the
os package does. The underlying cause of the error is available using the
Cause function.
os.IsNotExist(errors.Cause(err))
The result of the Error() call on an annotated error is the annotations joined
with colons, then the result of the Error() method for the underlying error
that was the cause.
err := errors.Errorf("original")
err = errors.Annotatef(err, "context")
err = errors.Annotatef(err, "more context")
err.Error() -> "more context: context: original"
Obviously recording the file, line and functions is not very useful if you cannot get them back out again.
errors.ErrorStack(err)
will return something like:
first error
github.com/juju/errors/annotation_test.go:193:
github.com/juju/errors/annotation_test.go:194: annotation
github.com/juju/errors/annotation_test.go:195:
github.com/juju/errors/annotation_test.go:196: more context
github.com/juju/errors/annotation_test.go:197:
The first error was generated by an external system, so there was no location associated. The second, fourth, and last lines were generated with Trace calls, and the other two through Annotate.
Sometimes when responding to an error you want to return a more specific error for the situation.
if err := FindField(field); err != nil {
return errors.Wrap(err, errors.NotFoundf(field))
}
This returns an error where the complete error stack is still available, and
errors.Cause() will return the NotFound error.
funcAlreadyExistsf(formatstring, args...interface{}) errorAlreadyExistsf returns an error which satisfies IsAlreadyExists().
funcAnnotate(othererror, messagestring) errorAnnotate is used to add extra context to an existing error. The location of the Annotate call is recorded with the annotations. The file, line and function are also recorded.
For example:
if err := SomeFunc(); err != nil {
return errors.Annotate(err, "failed to frombulate")
}
funcAnnotatef(othererror, formatstring, args...interface{}) errorAnnotatef is used to add extra context to an existing error. The location of the Annotate call is recorded with the annotations. The file, line and function are also recorded.
For example:
if err := SomeFunc(); err != nil {
return errors.Annotatef(err, "failed to frombulate the %s", arg)
}
funcBadRequestf(formatstring, args...interface{}) errorBadRequestf returns an error which satisfies IsBadRequest().
funcCause(errerror) errorCause returns the cause of the given error. This will be either the original error, or the result of a Wrap or Mask call.
Cause is the usual way to diagnose errors that may have been wrapped by the other errors functions.
funcDeferredAnnotatef(err*error, formatstring, args...interface{})DeferredAnnotatef annotates the given error (when it is not nil) with the given format string and arguments (like fmt.Sprintf). If *err is nil, DeferredAnnotatef does nothing. This method is used in a defer statement in order to annotate any resulting error with the same message.
For example:
defer DeferredAnnotatef(&err, "failed to frombulate the %s", arg)
funcDetails(errerror) stringDetails returns information about the stack of errors wrapped by err, in the format:
[{filename:99: error one} {otherfile:55: cause of error one}]
This is a terse alternative to ErrorStack as it returns a single line.
funcErrorStack(errerror) stringErrorStack returns a string representation of the annotated error. If the error passed as the parameter is not an annotated error, the result is simply the result of the Error() method on that error.
If the error is an annotated error, a multi-line string is returned where each line represents one entry in the annotation stack. The full filename from the call stack is used in the output.
first error
github.com/juju/errors/annotation_test.go:193:
github.com/juju/errors/annotation_test.go:194: annotation
github.com/juju/errors/annotation_test.go:195:
github.com/juju/errors/annotation_test.go:196: more context
github.com/juju/errors/annotation_test.go:197:
funcErrorf(formatstring, args...interface{}) errorErrorf creates a new annotated error and records the location that the error is created. This should be a drop in replacement for fmt.Errorf.
For example:
return errors.Errorf("validation failed: %s", message)
funcForbiddenf(formatstring, args...interface{}) errorForbiddenf returns an error which satistifes IsForbidden()
funcIsAlreadyExists(errerror) boolIsAlreadyExists reports whether the error was created with AlreadyExistsf() or NewAlreadyExists().
funcIsBadRequest(errerror) boolIsBadRequest reports whether err was created with BadRequestf() or NewBadRequest().
funcIsForbidden(errerror) boolIsForbidden reports whether err was created with Forbiddenf() or NewForbidden().
funcIsMethodNotAllowed(errerror) boolIsMethodNotAllowed reports whether err was created with MethodNotAllowedf() or NewMethodNotAllowed().
funcIsNotAssigned(errerror) boolIsNotAssigned reports whether err was created with NotAssignedf() or NewNotAssigned().
funcIsNotFound(errerror) boolIsNotFound reports whether err was created with NotFoundf() or NewNotFound().
funcIsNotImplemented(errerror) boolIsNotImplemented reports whether err was created with NotImplementedf() or NewNotImplemented().
funcIsNotProvisioned(errerror) boolIsNotProvisioned reports whether err was created with NotProvisionedf() or NewNotProvisioned().
funcIsNotSupported(errerror) boolIsNotSupported reports whether the error was created with NotSupportedf() or NewNotSupported().
funcIsNotValid(errerror) boolIsNotValid reports whether the error was created with NotValidf() or NewNotValid().
funcIsUnauthorized(errerror) boolIsUnauthorized reports whether err was created with Unauthorizedf() or NewUnauthorized().
funcIsUserNotFound(errerror) boolIsUserNotFound reports whether err was created with UserNotFoundf() or NewUserNotFound().
funcMask(othererror) errorMask hides the underlying error type, and records the location of the masking.
funcMaskf(othererror, formatstring, args...interface{}) errorMask masks the given error with the given format string and arguments (like fmt.Sprintf), returning a new error that maintains the error stack, but hides the underlying error type. The error string still contains the full annotations. If you want to hide the annotations, call Wrap.
funcMethodNotAllowedf(formatstring, args...interface{}) errorMethodNotAllowedf returns an error which satisfies IsMethodNotAllowed().
funcNew(messagestring) errorNew is a drop in replacement for the standard library errors module that records the location that the error is created.
For example:
return errors.New("validation failed")
funcNewAlreadyExists(errerror, msgstring) errorNewAlreadyExists returns an error which wraps err and satisfies IsAlreadyExists().
funcNewBadRequest(errerror, msgstring) errorNewBadRequest returns an error which wraps err that satisfies IsBadRequest().
funcNewForbidden(errerror, msgstring) errorNewForbidden returns an error which wraps err that satisfies IsForbidden().
funcNewMethodNotAllowed(errerror, msgstring) errorNewMethodNotAllowed returns an error which wraps err that satisfies IsMethodNotAllowed().
funcNewNotAssigned(errerror, msgstring) errorNewNotAssigned returns an error which wraps err that satisfies IsNotAssigned().
funcNewNotFound(errerror, msgstring) errorNewNotFound returns an error which wraps err that satisfies IsNotFound().
funcNewNotImplemented(errerror, msgstring) errorNewNotImplemented returns an error which wraps err and satisfies IsNotImplemented().
funcNewNotProvisioned(errerror, msgstring) errorNewNotProvisioned returns an error which wraps err that satisfies IsNotProvisioned().
funcNewNotSupported(errerror, msgstring) errorNewNotSupported returns an error which wraps err and satisfies IsNotSupported().
funcNewNotValid(errerror, msgstring) errorNewNotValid returns an error which wraps err and satisfies IsNotValid().
funcNewUnauthorized(errerror, msgstring) errorNewUnauthorized returns an error which wraps err and satisfies IsUnauthorized().
funcNewUserNotFound(errerror, msgstring) errorNewUserNotFound returns an error which wraps err and satisfies IsUserNotFound().
funcNotAssignedf(formatstring, args...interface{}) errorNotAssignedf returns an error which satisfies IsNotAssigned().
funcNotFoundf(formatstring, args...interface{}) errorNotFoundf returns an error which satisfies IsNotFound().
funcNotImplementedf(formatstring, args...interface{}) errorNotImplementedf returns an error which satisfies IsNotImplemented().
funcNotProvisionedf(formatstring, args...interface{}) errorNotProvisionedf returns an error which satisfies IsNotProvisioned().
funcNotSupportedf(formatstring, args...interface{}) errorNotSupportedf returns an error which satisfies IsNotSupported().
funcNotValidf(formatstring, args...interface{}) errorNotValidf returns an error which satisfies IsNotValid().
funcTrace(othererror) errorTrace adds the location of the Trace call to the stack. The Cause of the resulting error is the same as the error parameter. If the other error is nil, the result will be nil.
For example:
if err := SomeFunc(); err != nil {
return errors.Trace(err)
}
funcUnauthorizedf(formatstring, args...interface{}) errorUnauthorizedf returns an error which satisfies IsUnauthorized().
funcUserNotFoundf(formatstring, args...interface{}) errorUserNotFoundf returns an error which satisfies IsUserNotFound().
funcWrap(other, newDescriptiveerror) errorWrap changes the Cause of the error. The location of the Wrap call is also stored in the error stack.
For example:
if err := SomeFunc(); err != nil {
newErr := &packageError{"more context", private_value}
return errors.Wrap(err, newErr)
}
funcWrapf(other, newDescriptiveerror, formatstring, args...interface{}) errorWrapf changes the Cause of the error, and adds an annotation. The location of the Wrap call is also stored in the error stack.
For example:
if err := SomeFunc(); err != nil {
return errors.Wrapf(err, simpleErrorType, "invalid value %q", value)
}
typeErrstruct {
// contains filtered or unexported fields
}Err holds a description of an error along with information about where the error was created.
It may be embedded in custom error types to add extra information that this errors package can understand.
funcNewErr(formatstring, args...interface{}) ErrNewErr is used to return an Err for the purpose of embedding in other structures. The location is not specified, and needs to be set with a call to SetLocation.
For example:
type FooError struct {
errors.Err
code int
}
func NewFooError(code int) error {
err := &FooError{errors.NewErr("foo"), code}
err.SetLocation(1)
return err
}
funcNewErrWithCause(othererror, formatstring, args...interface{}) ErrNewErrWithCause is used to return an Err with cause by other error for the purpose of embedding in other structures. The location is not specified, and needs to be set with a call to SetLocation.
For example:
type FooError struct {
errors.Err
code int
}
func (e *FooError) Annotate(format string, args ...interface{}) error {
err := &FooError{errors.NewErrWithCause(e.Err, format, args...), e.code}
err.SetLocation(1)
return err
})
func (e*Err) Cause() errorThe Cause of an error is the most recent error in the error stack that meets one of these criteria: the original error that was raised; the new error that was passed into the Wrap function; the most recently masked error; or nil if the error itself is considered the Cause. Normally this method is not invoked directly, but instead through the Cause stand alone function.
func (e*Err) Error() stringError implements error.Error.
func (e*Err) Format(s fmt.State, verbrune)Format implements fmt.Formatter When printing errors with %+v it also prints the stack trace. %#v unsurprisingly will print the real underlying type.
func (e*Err) Location() (filenamestring, lineint)Location is the file and line of where the error was most recently created or annotated.
func (e*Err) Message() stringMessage returns the message stored with the most recent location. This is the empty string if the most recent call was Trace, or the message stored with Annotate or Mask.
func (e*Err) SetLocation(callDepthint)SetLocation records the source location of the error at callDepth stack frames above the call.
func (e*Err) StackTrace() []stringStackTrace returns one string for each location recorded in the stack of errors. The first value is the originating error, with a line for each other annotation or tracing of the error.
func (e*Err) Underlying() errorUnderlying returns the previous error in the error stack, if any. A client
should not ever really call this method. It is used to build the error
stack and should not be introspected by client calls. Or more
specifically, clients should not depend on anything but the Cause of an
error.
Generated by godoc2md