| 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 errorsscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/ |
| 10 | |
| 11 | import ( |
| 12 | "errorsscip-go gomod github.com/golang/go/src go1.17 errors/" |
| 13 | "fmtscip-go gomod github.com/golang/go/src go1.17 fmt/" |
| 14 | "stringsscip-go gomod github.com/golang/go/src go1.17 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 basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base# struct { |
| 19 | // info contains the error message passed through calls like errors.Wrap, errors.New. |
| 20 | infoscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#info. string |
| 21 | // stacktrace stores information about the program counters - i.e. where this error was generated. |
| 22 | stackscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#stack. stacktracescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/stacktrace# |
| 23 | // err is the actual error which is being wrapped with a stacktrace and message information. |
| 24 | errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#err. error |
| 25 | } |
| 26 | |
| 27 | // Error implements the error interface. |
| 28 | func (b *basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#) Errorscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#Error().() string { |
| 29 | if b.errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#err. != nil { |
| 30 | return fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("%s: %s", b.infoscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#info., b.errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#err..Error()) |
| 31 | } |
| 32 | return b.infoscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#info. |
| 33 | } |
| 34 | |
| 35 | // Unwrap implements the error Unwrap interface. |
| 36 | func (b *basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#) Unwrapscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#Unwrap().() error { |
| 37 | return b.errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#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 *basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#) Formatscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#Format().(s fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Statescip-go gomod github.com/golang/go/src go1.17 fmt/State#, verb rune) { |
| 43 | if verb == 'v' && s.Flagscip-go gomod github.com/golang/go/src go1.17 fmt/State#Flag().('+') { |
| 44 | s.Writescip-go gomod github.com/golang/go/src go1.17 fmt/State#Write().([]byte(formatErrorChainscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/formatErrorChain().(b))) |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | s.Writescip-go gomod github.com/golang/go/src go1.17 fmt/State#Write().([]byte(b.Errorscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#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 Newscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/New().(message string) error { |
| 55 | return &basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#{ |
| 56 | infoscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#info.: message, |
| 57 | stackscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#stack.: newStackTracescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/newStackTrace().(), |
| 58 | errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#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 Newfscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Newf().(format string, args ...interface{}) error { |
| 68 | return &basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#{ |
| 69 | infoscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#info.: fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().(format, args...), |
| 70 | stackscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#stack.: newStackTracescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/newStackTrace().(), |
| 71 | errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#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 Wrapscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Wrap().(cause error, message string) error { |
| 81 | if cause == nil { |
| 82 | return nil |
| 83 | } |
| 84 | |
| 85 | return &basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#{ |
| 86 | infoscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#info.: message, |
| 87 | stackscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#stack.: newStackTracescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/newStackTrace().(), |
| 88 | errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#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 Wrapfscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Wrapf().(cause error, format string, args ...interface{}) error { |
| 97 | if cause == nil { |
| 98 | return nil |
| 99 | } |
| 100 | return &basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#{ |
| 101 | infoscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#info.: fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().(format, args...), |
| 102 | stackscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#stack.: newStackTracescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/newStackTrace().(), |
| 103 | errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#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 Causescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/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 formatErrorChainscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/formatErrorChain().(err error) string { |
| 131 | var buf stringsscip-go gomod github.com/golang/go/src go1.17 strings/.Builderscip-go gomod github.com/golang/go/src go1.17 strings/Builder# |
| 132 | for err != nil { |
| 133 | if e, ok := err.(*basescip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#); ok { |
| 134 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.17 strings/Builder#WriteString().(fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("%s\n%v", e.infoscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#info., e.stackscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#stack.)) |
| 135 | err = e.errscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/base#err. |
| 136 | } else { |
| 137 | buf.WriteStringscip-go gomod github.com/golang/go/src go1.17 strings/Builder#WriteString().(fmtscip-go gomod github.com/golang/go/src go1.17 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.17 fmt/Sprintf().("%s\n", err.Error())) |
| 138 | err = nil |
| 139 | } |
| 140 | } |
| 141 | return buf.Stringscip-go gomod github.com/golang/go/src go1.17 strings/Builder#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 Isscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Is().(err, target error) bool { |
| 153 | return errorsscip-go gomod github.com/golang/go/src go1.17 errors/.Isscip-go gomod github.com/golang/go/src go1.17 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 Asscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/As().(err error, target interface{}) bool { |
| 160 | return errorsscip-go gomod github.com/golang/go/src go1.17 errors/.Asscip-go gomod github.com/golang/go/src go1.17 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 Unwrapscip-go gomod github.com/efficientgo/core . `github.com/efficientgo/core/errors`/Unwrap().(err error) error { |
| 167 | return errorsscip-go gomod github.com/golang/go/src go1.17 errors/.Unwrapscip-go gomod github.com/golang/go/src go1.17 errors/Unwrap().(err) |
| 168 | } |