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