1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
// Copyright 2014 Roger Peppe.
// See LICENCE file for details.
// The errors package provides a way to create and diagnose errors. It
// is compatible with the usual Go error idioms but adds a way to wrap
// errors so that they record source location information while
// retaining a consistent way for code to inspect errors to find out
// particular problems.
//
// An error created by this package holds three values, any one of
// which (but not all) may be zero
//
// - an error message. This is used as part of the result of the Error method.
// - an underlying error. This holds the error that the error was created in response to.
// - an error cause. See below.
//
// Error Causes
//
// The "cause" of an error is something that code can use to diagnose
// what an error means and take action accordingly.
//
// For example, if you use os.Remove to remove a file that is not there,
// it returns an error with a cause that satisfies os.IsNotExist. If you
// use filepath.Match to try to match a malformed pattern, it returns an
// error with a cause that equals filepath.ErrBadPattern.
//
// When the errors package wraps an error value with another one, the
// new error hides the cause by default. This is to prevent unintended
// dependencies between unrelated parts of the code base. When the cause
// is hidden, a caller can't write code that relies on the type or value
// of a particular cause which may well only be an implementation detail
// and subject to future change - a potentially API-breaking change if
// the cause is not hidden.
//
// The Note function can be used to preserve an existing error cause.
// The Because function can be used to associate an error with an
// existing cause value.
//
// Error wrapping
//
// When an error is returned that "wraps" another one, the new error
// records the source code location of the caller. This means that it is
// possible to extract this information later (by calling Details, for
// example). It is good practice to wrap errors whenever returning an
// error returned by a lower level function, so that the path taken by
// the error is recorded. This can make debugging significantly easier
// when there are many places an error could have come from.
package errors
// New returns a new error with the given error message and no cause. It
// is a drop-in replacement for errors.New from the standard library.
func New(s string) error {
return newError(nil, nil, s)
}
// Note returns a new error that wraps the given error. It preserves the cause if
// shouldPreserveCause is non-nil and shouldPreserveCause(err) is true. If msg is
// non-empty, it is used to prefix the returned error's Error value.
//
// If err is nil, Note returns nil and does not call shouldPreserveCause.
func Note(err error, shouldPreserveCause func(error) bool, msg string) error {
if err == nil {
return nil
}
if shouldPreserveCause == nil {
return newError(err, nil, msg)
}
if cause := Cause(err); shouldPreserveCause(cause) {
return newError(err, cause, msg)
}
return newError(err, nil, msg)
}
// Because returns a new error that wraps err and has the given cause,
// and adds the given message.
//
// If err is nil and msg is empty, the returned error's message will be
// the same as cause's. This is equivalent to calling errors.Note(cause,
// errors.Is(cause), "") and is useful for annotating an global error
// value with source location information.
//
// Because returns a nil error if all of err, cause and msg are
// zero.
func Because(err, cause error, msg string) error {
if err == nil && msg == "" {
if cause == nil {
return nil
}
msg = cause.Error()
}
return newError(err, cause, msg)
}
// Wrap returns a new error that wraps the given error and holds
// information about the caller. It is equivalent to Note(err, nil, "").
// Note that this means that the returned error has no cause.
func Wrap(err error) error {
if err == nil {
return nil
}
return newError(err, nil, "")
}
// Cause returns the cause of the given error. If err does not
// implement Causer or its Cause method returns nil, it returns err itself.
//
// Cause is the usual way to diagnose errors that may have
// been wrapped.
func Cause(err error) error {
if err, ok := err.(Causer); ok {
if cause := err.Cause(); cause != nil {
return cause
}
}
return err
}
// SetLocation sets the location of the error to the file and line
// number of the code that is running callDepth frames above the caller.
// It does nothing if the error was not created by this package.
// SetLocation should only be called immediately after an error has been
// created, before it is shared with other code which could access it
// concurrently.
//
// This function is helpful when a helper function is used to
// create an error and the caller of the helper function is
// the important thing, not the location in the helper function itself.
func SetLocation(err error, callDepth int) {
if err, ok := err.(*errorInfo); ok {
err.setLocation(callDepth + 1)
}
}
// Details returns information about the stack of underlying errors
// wrapped by err, in the format:
//
// [
// {filename:99: error one}
// {otherfile:55: cause of error one}
// ]
//
// The details are found by type-asserting the error to the Locator,
// Causer and Wrapper interfaces. Details of the underlying stack are
// found by recursively calling Underlying when the underlying error
// implements Wrapper.
func Details(err error) string {
if err == nil {
return "[]"
}
s := make([]byte, 0, 30)
s = append(s, '[')
for {
s = append(s, "\n\t{"...)
if err, ok := err.(Locator); ok {
file, line := err.Location()
if file != "" {
s = append(s, file...)
s = append(s, ':')
s = appendInt(s, line)
s = append(s, ": "...)
}
}
if cerr, ok := err.(Wrapper); ok {
s = append(s, cerr.Message()...)
err = cerr.Underlying()
} else {
s = append(s, err.Error()...)
err = nil
}
s = append(s, '}')
if err == nil {
break
}
}
s = append(s, "\n]"...)
return string(s)
}
// Is returns a function that returns whether the an error is equal to
// the given error. It is intended to be used as a "shouldPreserveCause"
// argument to Note. For example:
//
// return errgo.Note(err, errgo.Is(http.ErrNoCookie), "")
//
// would return an error with an http.ErrNoCookie cause
// only if that was err's cause.
func Is(err error) func(error) bool {
return func(err1 error) bool {
return err == err1
}
}
// Any returns true. It can be used as an argument to Note
// to allow any cause to pass through to the wrapped
// error.
func Any(error) bool {
return true
}
// OneOf returns a function suitable for passing to Note
// that reports whether an error matches any of the
// given predicates.
func OneOf(predicates ...func(error) bool) func(error) bool {
return func(err error) bool {
for _, predicate := range predicates {
if predicate(err) {
return true
}
}
return false
}
}
|