3
import "github.com/juju/errors"
5
[![GoDoc](https://godoc.org/github.com/juju/errors?status.svg)](https://godoc.org/github.com/juju/errors)
7
The juju/errors provides an easy way to annotate errors without losing the
10
The exported `New` and `Errorf` functions are designed to replace the
11
`errors.New` and `fmt.Errorf` functions respectively. The same underlying
12
error is there, but the package also records the location at which the error
15
A primary use case for this library is to add extra context any time an
16
error is returned from a function.
19
if err := SomeFunc(); err != nil {
26
if err := SomeFunc(); err != nil {
27
return errors.Trace(err)
30
which just records the file and line number of the Trace call, or
33
if err := SomeFunc(); err != nil {
34
return errors.Annotate(err, "more context")
37
which also adds an annotation to the error.
39
When you want to check to see if an error is of a particular type, a helper
40
function is normally exported by the package that returned the error, like the
41
`os` package does. The underlying cause of the error is available using the
45
os.IsNotExist(errors.Cause(err))
47
The result of the `Error()` call on an annotated error is the annotations joined
48
with colons, then the result of the `Error()` method for the underlying error
52
err := errors.Errorf("original")
53
err = errors.Annotatef(err, "context")
54
err = errors.Annotatef(err, "more context")
55
err.Error() -> "more context: context: original"
57
Obviously recording the file, line and functions is not very useful if you
58
cannot get them back out again.
61
errors.ErrorStack(err)
63
will return something like:
67
github.com/juju/errors/annotation_test.go:193:
68
github.com/juju/errors/annotation_test.go:194: annotation
69
github.com/juju/errors/annotation_test.go:195:
70
github.com/juju/errors/annotation_test.go:196: more context
71
github.com/juju/errors/annotation_test.go:197:
73
The first error was generated by an external system, so there was no location
74
associated. The second, fourth, and last lines were generated with Trace calls,
75
and the other two through Annotate.
77
Sometimes when responding to an error you want to return a more specific error
81
if err := FindField(field); err != nil {
82
return errors.Wrap(err, errors.NotFoundf(field))
85
This returns an error where the complete error stack is still available, and
86
`errors.Cause()` will return the `NotFound` error.
93
## func AlreadyExistsf
95
func AlreadyExistsf(format string, args ...interface{}) error
97
AlreadyExistsf returns an error which satisfies IsAlreadyExists().
102
func Annotate(other error, message string) error
104
Annotate is used to add extra context to an existing error. The location of
105
the Annotate call is recorded with the annotations. The file, line and
106
function are also recorded.
111
if err := SomeFunc(); err != nil {
112
return errors.Annotate(err, "failed to frombulate")
118
func Annotatef(other error, format string, args ...interface{}) error
120
Annotatef is used to add extra context to an existing error. The location of
121
the Annotate call is recorded with the annotations. The file, line and
122
function are also recorded.
127
if err := SomeFunc(); err != nil {
128
return errors.Annotatef(err, "failed to frombulate the %s", arg)
134
func Cause(err error) error
136
Cause returns the cause of the given error. This will be either the
137
original error, or the result of a Wrap or Mask call.
139
Cause is the usual way to diagnose errors that may have been wrapped by
140
the other errors functions.
143
## func DeferredAnnotatef
145
func DeferredAnnotatef(err *error, format string, args ...interface{})
147
DeferredAnnotatef annotates the given error (when it is not nil) with the given
148
format string and arguments (like fmt.Sprintf). If *err is nil, DeferredAnnotatef
149
does nothing. This method is used in a defer statement in order to annotate any
150
resulting error with the same message.
155
defer DeferredAnnotatef(&err, "failed to frombulate the %s", arg)
160
func Details(err error) string
162
Details returns information about the stack of errors wrapped by err, in
166
[{filename:99: error one} {otherfile:55: cause of error one}]
168
This is a terse alternative to ErrorStack as it returns a single line.
173
func ErrorStack(err error) string
175
ErrorStack returns a string representation of the annotated error. If the
176
error passed as the parameter is not an annotated error, the result is
177
simply the result of the Error() method on that error.
179
If the error is an annotated error, a multi-line string is returned where
180
each line represents one entry in the annotation stack. The full filename
181
from the call stack is used in the output.
185
github.com/juju/errors/annotation_test.go:193:
186
github.com/juju/errors/annotation_test.go:194: annotation
187
github.com/juju/errors/annotation_test.go:195:
188
github.com/juju/errors/annotation_test.go:196: more context
189
github.com/juju/errors/annotation_test.go:197:
194
func Errorf(format string, args ...interface{}) error
196
Errorf creates a new annotated error and records the location that the
197
error is created. This should be a drop in replacement for fmt.Errorf.
202
return errors.Errorf("validation failed: %s", message)
205
## func IsAlreadyExists
207
func IsAlreadyExists(err error) bool
209
IsAlreadyExists reports whether the error was created with
210
AlreadyExistsf() or NewAlreadyExists().
215
func IsNotFound(err error) bool
217
IsNotFound reports whether err was created with NotFoundf() or
221
## func IsNotImplemented
223
func IsNotImplemented(err error) bool
225
IsNotImplemented reports whether err was created with
226
NotImplementedf() or NewNotImplemented().
229
## func IsNotSupported
231
func IsNotSupported(err error) bool
233
IsNotSupported reports whether the error was created with
234
NotSupportedf() or NewNotSupported().
239
func IsNotValid(err error) bool
241
IsNotValid reports whether the error was created with NotValidf() or
245
## func IsUnauthorized
247
func IsUnauthorized(err error) bool
249
IsUnauthorized reports whether err was created with Unauthorizedf() or
255
func Mask(other error) error
257
Mask hides the underlying error type, and records the location of the masking.
262
func Maskf(other error, format string, args ...interface{}) error
264
Mask masks the given error with the given format string and arguments (like
265
fmt.Sprintf), returning a new error that maintains the error stack, but
266
hides the underlying error type. The error string still contains the full
267
annotations. If you want to hide the annotations, call Wrap.
272
func New(message string) error
274
New is a drop in replacement for the standard libary errors module that records
275
the location that the error is created.
280
return errors.New("validation failed")
283
## func NewAlreadyExists
285
func NewAlreadyExists(err error, msg string) error
287
NewAlreadyExists returns an error which wraps err and satisfies
293
func NewNotFound(err error, msg string) error
295
NewNotFound returns an error which wraps err that satisfies
299
## func NewNotImplemented
301
func NewNotImplemented(err error, msg string) error
303
NewNotImplemented returns an error which wraps err and satisfies
307
## func NewNotSupported
309
func NewNotSupported(err error, msg string) error
311
NewNotSupported returns an error which wraps err and satisfies
317
func NewNotValid(err error, msg string) error
319
NewNotValid returns an error which wraps err and satisfies IsNotValid().
322
## func NewUnauthorized
324
func NewUnauthorized(err error, msg string) error
326
NewUnauthorized returns an error which wraps err and satisfies
332
func NotFoundf(format string, args ...interface{}) error
334
NotFoundf returns an error which satisfies IsNotFound().
337
## func NotImplementedf
339
func NotImplementedf(format string, args ...interface{}) error
341
NotImplementedf returns an error which satisfies IsNotImplemented().
344
## func NotSupportedf
346
func NotSupportedf(format string, args ...interface{}) error
348
NotSupportedf returns an error which satisfies IsNotSupported().
353
func NotValidf(format string, args ...interface{}) error
355
NotValidf returns an error which satisfies IsNotValid().
360
func Trace(other error) error
362
Trace adds the location of the Trace call to the stack. The Cause of the
363
resulting error is the same as the error parameter. If the other error is
364
nil, the result will be nil.
369
if err := SomeFunc(); err != nil {
370
return errors.Trace(err)
374
## func Unauthorizedf
376
func Unauthorizedf(format string, args ...interface{}) error
378
Unauthorizedf returns an error which satisfies IsUnauthorized().
383
func Wrap(other, newDescriptive error) error
385
Wrap changes the Cause of the error. The location of the Wrap call is also
386
stored in the error stack.
391
if err := SomeFunc(); err != nil {
392
newErr := &packageError{"more context", private_value}
393
return errors.Wrap(err, newErr)
399
func Wrapf(other, newDescriptive error, format string, args ...interface{}) error
401
Wrapf changes the Cause of the error, and adds an annotation. The location
402
of the Wrap call is also stored in the error stack.
407
if err := SomeFunc(); err != nil {
408
return errors.Wrapf(err, simpleErrorType, "invalid value %q", value)
416
// contains filtered or unexported fields
419
Err holds a description of an error along with information about
420
where the error was created.
422
It may be embedded in custom error types to add extra information that
423
this errors package can understand.
435
func NewErr(format string, args ...interface{}) Err
437
NewErr is used to return an Err for the purpose of embedding in other
438
structures. The location is not specified, and needs to be set with a call
444
type FooError struct {
449
func NewFooError(code int) error {
450
err := &FooError{errors.NewErr("foo"), code}
458
### func (\*Err) Cause
460
func (e *Err) Cause() error
462
The Cause of an error is the most recent error in the error stack that
463
meets one of these criteria: the original error that was raised; the new
464
error that was passed into the Wrap function; the most recently masked
465
error; or nil if the error itself is considered the Cause. Normally this
466
method is not invoked directly, but instead through the Cause stand alone
471
### func (\*Err) Error
473
func (e *Err) Error() string
475
Error implements error.Error.
479
### func (\*Err) Location
481
func (e *Err) Location() (filename string, line int)
483
Location is the file and line of where the error was most recently
484
created or annotated.
488
### func (\*Err) Message
490
func (e *Err) Message() string
492
Message returns the message stored with the most recent location. This is
493
the empty string if the most recent call was Trace, or the message stored
494
with Annotate or Mask.
498
### func (\*Err) SetLocation
500
func (e *Err) SetLocation(callDepth int)
502
SetLocation records the source location of the error at callDepth stack
503
frames above the call.
507
### func (\*Err) StackTrace
509
func (e *Err) StackTrace() []string
511
StackTrace returns one string for each location recorded in the stack of
512
errors. The first value is the originating error, with a line for each
513
other annotation or tracing of the error.
517
### func (\*Err) Underlying
519
func (e *Err) Underlying() error
521
Underlying returns the previous error in the error stack, if any. A client
522
should not ever really call this method. It is used to build the error
523
stack and should not be introspected by client calls. Or more
524
specifically, clients should not depend on anything but the `Cause` of an
536
Generated by [godoc2md](http://godoc.org/github.com/davecheney/godoc2md)
b'\\ No newline at end of file'