| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | // Initially copied from Thanos and contributed by https://github.com/bisakhmondal. |
| 5 | // |
| 6 | // Copyright (c) The Thanos Authors. |
| 7 | // Licensed under the Apache License 2.0. |
| 8 | |
| 9 | package errors |
| 10 | |
| 11 | import ( |
| 12 | "errors" |
| 13 | "fmt" |
| 14 | "strings" |
| 15 | ) |
| 16 | |
| 17 | // base is the fundamental struct that implements the error interface and the acts as the backbone of this errors package. |
| 18 | type base struct { |
| 19 | // info contains the error message passed through calls like errors.Wrap, errors.New. |
| 20 | info string |
| 21 | // stacktrace stores information about the program counters - i.e. where this error was generated. |
| 22 | stack stacktrace |
| 23 | // err is the actual error which is being wrapped with a stacktrace and message information. |
| 24 | err error |
| 25 | } |
| 26 | |
| 27 | // Error implements the error interface. |
| 28 | func (b *base) Error() string { |
| 29 | if b.err != nil { |
| 30 | return fmt.Sprintf("%s: %s", b.info, b.err.Error()) |
| 31 | } |
| 32 | return b.info |
| 33 | } |
| 34 | |
| 35 | // Unwrap implements the error Unwrap interface. |
| 36 | func (b *base) Unwrap() error { |
| 37 | return b.err |
| 38 | } |
| 39 | |
| 40 | // Format implements the fmt.Formatter interface to support the formatting of an error chain with "%+v" verb. |
| 41 | // Whenever error is printed with %+v format verb, stacktrace info gets dumped to the output. |
| 42 | func (b *base) Format(s fmt.State, verb rune) { |
| 43 | if verb == 'v' && s.Flag('+') { |
| 44 | s.Write([]byte(formatErrorChain(b))) |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | s.Write([]byte(b.Error())) |
| 49 | } |
| 50 | |
| 51 | // New returns a new error with a stacktrace of recent call frames. Each call to New |
| 52 | // returns a distinct error value even if the text is identical. An alternative of |
| 53 | // the errors.New function from github.com/pkg/errors. |
| 54 | func New(message string) error { |
| 55 | return &base{ |
| 56 | info: message, |
| 57 | stack: newStackTrace(), |
| 58 | err: nil, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // Newf is like New, but it formats input according to a format specifier. |
| 63 | // An alternative of the fmt.Errorf function. |
| 64 | // |
| 65 | // If no args have been passed, it is same as `New` function without formatting. Character like |
| 66 | // '%' still has to be escaped in that scenario. |
| 67 | func Newf(format string, args ...interface{}) error { |
| 68 | return &base{ |
| 69 | info: fmt.Sprintf(format, args...), |
| 70 | stack: newStackTrace(), |
| 71 | err: nil, |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // Wrap returns a new error, which wraps another error with a stacktrace containing recent call frames. |
| 76 | // |
| 77 | // If cause is nil, it does not produce the error, similar to errors.Wrap from github.com/pkg/errors was doing. |
| 78 | // Still, avoid `errors.Wrap(nil, "...") patterns as it can lead to inefficiencies while constructing arguments |
| 79 | // to format as well readability issues. We keep this behaviour to make sure it's a drop-in replacement. |
| 80 | func Wrap(cause error, message string) error { |
| 81 | if cause == nil { |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | return &base{ |
| 86 | info: message, |
| 87 | stack: newStackTrace(), |
| 88 | err: cause, |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Wrapf is like Wrap but the message is formatted with the supplied format specifier. |
| 93 | // |
| 94 | // If no args have been passed, it is same as `Wrap` function without formatting. |
| 95 | // Character like '%' still has to be escaped in that scenario. |
| 96 | func Wrapf(cause error, format string, args ...interface{}) error { |
| 97 | if cause == nil { |
| 98 | return nil |
| 99 | } |
| 100 | return &base{ |
| 101 | info: fmt.Sprintf(format, args...), |
| 102 | stack: newStackTrace(), |
| 103 | err: cause, |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Cause returns the result of repeatedly calling the Unwrap method on err, if err's |
| 108 | // type implements an Unwrap method. Otherwise, Cause returns the last encountered error. |
| 109 | // The difference between Unwrap and Cause is the first one performs unwrapping of one level |
| 110 | // but Cause returns the last err (whether it's nil or not) where it failed to assert |
| 111 | // the interface containing the Unwrap method. |
| 112 | // This is a replacement of errors.Cause without the causer interface from pkg/errors which |
| 113 | // actually can be sufficed through the errors.Is function. But considering some use cases |
| 114 | // where we need to peel off all the external layers applied through errors.Wrap family, |
| 115 | // it is useful ( where external SDK doesn't use errors.Is internally). |
| 116 | func Cause(err error) error { |
| 117 | for err != nil { |
| 118 | e, ok := err.(interface { |
| 119 | Unwrap() error |
| 120 | }) |
| 121 | if !ok { |
| 122 | return err |
| 123 | } |
| 124 | err = e.Unwrap() |
| 125 | } |
| 126 | return nil |
| 127 | } |
| 128 | |
| 129 | // formatErrorChain formats an error chain. |
| 130 | func formatErrorChain(err error) string { |
| 131 | var buf strings.Builder |
| 132 | for err != nil { |
| 133 | if e, ok := err.(*base); ok { |
| 134 | buf.WriteString(fmt.Sprintf("%s\n%v", e.info, e.stack)) |
| 135 | err = e.err |
| 136 | } else { |
| 137 | buf.WriteString(fmt.Sprintf("%s\n", err.Error())) |
| 138 | err = nil |
| 139 | } |
| 140 | } |
| 141 | return buf.String() |
| 142 | } |
| 143 | |
| 144 | // The functions `Is`, `As` & `Unwrap` provides a thin wrapper around the builtin errors |
| 145 | // package in go. Just for sake of completeness and correct autocompletion behaviors from |
| 146 | // IDEs they have been wrapped using functions instead of using variable to reference them |
| 147 | // as first class functions (eg: var Is = errros.Is ). |
| 148 | |
| 149 | // Is is a wrapper of built-in errors.Is. It reports whether any error in err's |
| 150 | // chain matches target. The chain consists of err itself followed by the sequence |
| 151 | // of errors obtained by repeatedly calling Unwrap. |
| 152 | func Is(err, target error) bool { |
| 153 | return errors.Is(err, target) |
| 154 | } |
| 155 | |
| 156 | // As is a wrapper of built-in errors.As. It finds the first error in err's |
| 157 | // chain that matches target, and if one is found, sets target to that error |
| 158 | // value and returns true. Otherwise, it returns false. |
| 159 | func As(err error, target interface{}) bool { |
| 160 | return errors.As(err, target) |
| 161 | } |
| 162 | |
| 163 | // Unwrap is a wrapper of built-in errors.Unwrap. Unwrap returns the result of |
| 164 | // calling the Unwrap method on err, if err's type contains an Unwrap method |
| 165 | // returning error. Otherwise, Unwrap returns nil. |
| 166 | func Unwrap(err error) error { |
| 167 | return errors.Unwrap(err) |
| 168 | } |