3
import "gopkg.in/errgo.v1"
5
The errgo package provides a way to create and diagnose errors. It is compatible
6
with the usual Go error idioms but adds a way to wrap errors so that they record
7
source location information while retaining a consistent way for code to inspect
8
errors to find out particular problems.
17
Any returns true. It can be used as an argument to Mask to allow any diagnosis
18
to pass through to the wrapped error.
23
func Cause(err error) error
25
Cause returns the cause of the given error. If err does not implement Causer or
26
its Cause method returns nil, it returns err itself.
28
Cause is the usual way to diagnose errors that may have been wrapped by Mask or
34
func Details(err error) string
36
Details returns information about the stack of underlying errors wrapped by err,
39
[{filename:99: error one} {otherfile:55: cause of error one}]
41
The details are found by type-asserting the error to the Locationer, Causer and
42
Wrapper interfaces. Details of the underlying stack are found by recursively
43
calling Underlying when the underlying error implements Wrapper.
48
func Is(err error) func(error) bool
50
Is returns a function that returns whether the an error is equal to the given
51
error. It is intended to be used as a "pass" argument to Mask and friends; for
54
return errors.Mask(err, errors.Is(http.ErrNoCookie))
56
would return an error with an http.ErrNoCookie cause only if that was err's
57
diagnosis; otherwise the diagnosis would be itself.
62
func Mask(underlying error, pass ...func(error) bool) error
64
Mask returns an Err that wraps the given underyling error. The error message is
65
unchanged, but the error location records the caller of Mask.
67
If err is nil, Mask returns nil.
69
By default Mask conceals the cause of the wrapped error, but if pass(Cause(err))
70
returns true for any of the provided pass functions, the cause of the returned
71
error will be Cause(err).
73
For example, the following code will return an error whose cause is the error
74
from the os.Open call when (and only when) the file does not exist.
76
f, err := os.Open("non-existent-file")
78
return errors.Mask(err, os.IsNotExist)
81
In order to add context to returned errors, it is conventional to call Mask when
82
returning any error received from elsewhere.
87
func MaskFunc(allow ...func(error) bool) func(error, ...func(error) bool) error
89
MaskFunc returns an equivalent of Mask that always allows the specified causes
90
in addition to any causes specified when the returned function is called.
92
It is defined for convenience, for example when all calls to Mask in a given
93
package wish to allow the same set of causes to be returned.
98
func New(s string) error
100
New returns a new error with the given error message and no cause. It is a
101
drop-in replacement for errors.New from the standard library.
106
func Newf(f string, a ...interface{}) error
108
Newf returns a new error with the given printf-formatted error message and no
114
func NoteMask(underlying error, msg string, pass ...func(error) bool) error
116
NoteMask returns an Err that has the given underlying error, with the given
117
message added as context, and allowing the cause of the underlying error to pass
118
through into the result if allowed by the specific pass functions (see Mask for
119
an explanation of the pass parameter).
124
func Notef(underlying error, f string, a ...interface{}) error
126
Notef returns an Error that wraps the given underlying error and adds the given
127
formatted context message. The returned error has no cause (use NoteMask or
128
WithCausef to add a message while retaining a cause).
133
func WithCausef(underlying, cause error, f string, a ...interface{}) error
135
WithCausef returns a new Error that wraps the given (possibly nil) underlying
136
error and associates it with the given cause. The given formatted message
137
context will also be added.
142
type Causer interface {
147
Causer is the type of an error that may provide an error cause for error
148
diagnosis. Cause may return nil if there is no cause (for example because the
149
cause has been masked).
155
// Message_ holds the text of the error message. It may be empty
156
// if Underlying is set.
159
// Cause_ holds the cause of the error as returned
160
// by the Cause method.
163
// Underlying holds the underlying error, if any.
166
// File and Line identify the source code location where the error was
173
Err holds a description of an error along with information about where the error
176
It may be embedded in custom error types to add extra information that this
177
errors package can understand.
179
#### func (*Err) Cause
182
func (e *Err) Cause() error
184
Cause implements Causer.
186
#### func (*Err) Error
189
func (e *Err) Error() string
191
Error implements error.Error.
193
#### func (*Err) GoString
196
func (e *Err) GoString() string
198
GoString returns the details of the receiving error message, so that printing an
199
error with %#v will produce useful information.
201
#### func (*Err) Location
204
func (e *Err) Location() (file string, line int)
206
Location implements Locationer.
208
#### func (*Err) Message
211
func (e *Err) Message() string
213
Message returns the top level error message.
215
#### func (*Err) SetLocation
218
func (e *Err) SetLocation(callDepth int)
220
Locate records the source location of the error by setting e.Location, at
221
callDepth stack frames above the call.
223
#### func (*Err) Underlying
226
func (e *Err) Underlying() error
228
Underlying returns the underlying error if any.
233
type Locationer interface {
234
// Location returns the name of the file and the line
235
// number associated with an error.
236
Location() (file string, line int)
240
Locationer can be implemented by any error type that wants to expose the source
241
location of an error.
246
type Wrapper interface {
247
// Message returns the top level error message,
248
// not including the message from the underlying
252
// Underlying returns the underlying error, or nil
258
Wrapper is the type of an error that wraps another error. It is exposed so that
259
external types may implement it, but should in general not be used otherwise.