Documentation ¶
Overview ¶
The juju/errors provides an easy way to annotate errors without losing the orginal error context.
The package is based on github.com/juju/errgo and embeds the errgo.Err type.
The exported New and Errorf functions is 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.
Often when you want to check to see if an error is of a particular type, a helper function is exported by the package that returned the error, like the `os` package. The underlying cause of the error is available using the Cause function, or you can test the cause with the Check function.
os.IsNotExist(errors.Cause(err)) errors.Check(err, os.IsNotExist)
The result of the Error() call on the 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("context") err = errors.Annotatef("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.
If you are creating the errors, you can simply call:
errors.Errorf("format just like fmt.Errorf")
This function will return an error that contains the annotation stack and records the file, line and function from the place where the error is created.
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.
Index ¶
- func AlreadyExistsf(format string, args ...interface{}) error
- func Annotate(other error, message string) error
- func Annotatef(other error, format string, args ...interface{}) error
- func Cause(err error) error
- func Check(err error, checker func(error) bool) bool
- func Contextf(err *error, format string, args ...interface{})
- func ErrorStack(err error) string
- func Errorf(format string, args ...interface{}) error
- func IsAlreadyExists(err error) bool
- func IsNotFound(err error) bool
- func IsNotImplemented(err error) bool
- func IsNotSupported(err error) bool
- func IsUnauthorized(err error) bool
- func LoggedErrorf(logger loggo.Logger, format string, a ...interface{}) error
- func Maskf(err *error, format string, args ...interface{})
- func New(message string) error
- func NewAlreadyExists(err error, msg string) error
- func NewNotFound(err error, msg string) error
- func NewNotImplemented(err error, msg string) error
- func NewNotSupported(err error, msg string) error
- func NewUnauthorized(err error, msg string) error
- func NotFoundf(format string, args ...interface{}) error
- func NotImplementedf(format string, args ...interface{}) error
- func NotSupportedf(format string, args ...interface{}) error
- func Trace(other error) error
- func Unauthorizedf(format string, args ...interface{}) error
- func Wrap(other, newDescriptive error) error
- type Err
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AlreadyExistsf ¶
AlreadyExistsf returns an error which satisfies IsAlreadyExists().
func Annotate ¶
Annotate 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. If the error argument is nil, the result of Annotate is nil.
For example:
if err := SomeFunc(); err != nil { return errors.Annotate(err, "failed to frombulate") }
func Annotatef ¶
Annotatef 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. If the error argument is nil, the result of Annotatef is nil.
For example:
if err := SomeFunc(); err != nil { return errors.Annotatef(err, "failed to frombulate the %s", arg) }
func Cause ¶
Cause returns the cause of the given error. If err does not implement errgo.Causer or its Cause method returns nil, it returns err itself.
func Check ¶
Check looks at the Cause of the error to see if it matches the checker function.
For example:
if err := SomeFunc(); err != nil { if errors.Check(err, os.IsNotExist) { return someOtherFunc() } }
func Contextf ¶
Contextf prefixes any error stored in err with text formatted according to the format specifier. If err does not contain an error, Contextf does nothing. All errors created with functions from this package are preserved when wrapping.
func ErrorStack ¶
ErrorStack 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.
func Errorf ¶
Errorf 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)
func IsAlreadyExists ¶
IsAlreadyExists reports whether the error was created with AlreadyExistsf() or NewAlreadyExists().
func IsNotFound ¶
IsNotFound reports whether err was created with NotFoundf() or NewNotFound().
func IsNotImplemented ¶
IsNotImplemented reports whether err was created with NotImplementedf() or NewNotImplemented().
func IsNotSupported ¶
IsNotSupported reports whether the error was created with NotSupportedf() or NewNotSupported().
func IsUnauthorized ¶
IsUnauthorized reports whether err was created with Unauthorizedf() or NewUnauthorized().
func LoggedErrorf ¶
LoggedErrorf logs the error and return an error with the same text.
func Maskf ¶
Maskf masks the given error (when it is not nil) with the given format string and arguments (like fmt.Sprintf), returning a new error. If *err is nil, Maskf does nothing.
func New ¶
New is a drop in replacement for the standard libary errors module that records the location that the error is created.
For example:
return errors.New("validation failed")
func NewAlreadyExists ¶
NewAlreadyExists returns an error which wraps err and satisfies IsAlreadyExists().
func NewNotFound ¶
NewNotFound returns an error which wraps err that satisfies IsNotFound().
func NewNotImplemented ¶
NewNotImplemented returns an error which wraps err and satisfies IsNotImplemented().
func NewNotSupported ¶
NewNotSupported returns an error which wraps err and satisfies IsNotSupported().
func NewUnauthorized ¶
NewUnauthorized returns an error which wraps err and satisfies IsUnauthorized().
func NotImplementedf ¶
NotImplementedf returns an error which satisfies IsNotImplemented().
func NotSupportedf ¶
NotSupportedf returns an error which satisfies IsNotSupported().
func Trace ¶
Trace always returns an annotated error. Trace records the location of the Trace call, and adds it to the annotation stack. If the error argument is nil, the result of Trace is nil.
For example:
if err := SomeFunc(); err != nil { return errors.Trace(err) }
Example ¶
package main import ( "fmt" "github.com/juju/errors" ) func main() { var err1 error = fmt.Errorf("something wicked this way comes") var err2 error = nil // Tracing a non nil error will return an error fmt.Println(errors.Trace(err1)) // Tracing nil will return nil fmt.Println(errors.Trace(err2)) }
Output: something wicked this way comes <nil>
func Unauthorizedf ¶
Unauthorizedf returns an error which satisfies IsUnauthorized().
func Wrap ¶
Wrap changes the error value that is returned with LastError. The location of the Wrap call is also stored in the annotation stack. If the error argument is nil, the result of Wrap is nil.
For example:
if err := SomeFunc(); err != nil { newErr := &packageError{"more context", private_value} return errors.Wrap(err, newErr) }