| 1 | // Copyright (c) The EfficientGo Authors. |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | package merrors |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | stderrors "errors" |
| 9 | "fmt" |
| 10 | "io" |
| 11 | ) |
| 12 | |
| 13 | // NilOrMultiError type allows combining multiple errors into one. |
| 14 | type NilOrMultiError struct { |
| 15 | errs []error |
| 16 | } |
| 17 | |
| 18 | // New returns NilOrMultiError with provided errors added if not nil. |
| 19 | func New(errs ...error) *NilOrMultiError { |
| 20 | m := &NilOrMultiError{} |
| 21 | m.Add(errs...) |
| 22 | return m |
| 23 | } |
| 24 | |
| 25 | // Add adds single or many errors to the error list. Each error is added only if not nil. |
| 26 | // If the error is a multiError type, the errors inside multiError are added to the main NilOrMultiError. |
| 27 | func (e *NilOrMultiError) Add(errs ...error) { |
| 28 | for _, err := range errs { |
| 29 | if err == nil { |
| 30 | continue |
| 31 | } |
| 32 | if merr, ok := err.(multiError); ok { |
| 33 | e.errs = append(e.errs, merr.errs...) |
| 34 | continue |
| 35 | } |
| 36 | e.errs = append(e.errs, err) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Err returns the error list as an Error (also implements error) or nil if it is empty. |
| 41 | func (e NilOrMultiError) Err() Error { |
| 42 | if len(e.errs) == 0 { |
| 43 | return nil |
| 44 | } |
| 45 | return multiError(e) |
| 46 | } |
| 47 | |
| 48 | // Error is extended error interface that allows to use returned read-only multi error in more advanced ways. |
| 49 | type Error interface { |
| 50 | error |
| 51 | |
| 52 | // Errors returns underlying errors. |
| 53 | Errors() []error |
| 54 | |
| 55 | // As finds the first error in multiError slice of error chains that matches target, and if so, sets |
| 56 | // target to that error value and returns true. Otherwise, it returns false. |
| 57 | // |
| 58 | // An error matches target if the error's concrete value is assignable to the value |
| 59 | // pointed to by target, or if the error has a method As(interface{}) bool such that |
| 60 | // As(target) returns true. In the latter case, the As method is responsible for |
| 61 | // setting target. |
| 62 | As(target interface{}) bool |
| 63 | // Is returns true if any error in multiError's slice of error chains matches the given target or |
| 64 | // if the target is of multiError type. |
| 65 | // |
| 66 | // An error is considered to match a target if it is equal to that target or if |
| 67 | // it implements a method Is(error) bool such that Is(target) returns true. |
| 68 | Is(target error) bool |
| 69 | // Count returns the number of multi error' errors that match the given target. |
| 70 | // Matching is defined as in Is method. |
| 71 | Count(target error) int |
| 72 | } |
| 73 | |
| 74 | // multiError implements the error and Error interfaces, and it represents NilOrMultiError (in other words []error) with at least one error inside it. |
| 75 | // NOTE: This type is useful to make sure that NilOrMultiError is not accidentally used for err != nil check. |
| 76 | type multiError struct { |
| 77 | errs []error |
| 78 | } |
| 79 | |
| 80 | // Errors returns underlying errors. |
| 81 | func (e multiError) Errors() []error { |
| 82 | return e.errs |
| 83 | } |
| 84 | |
| 85 | // Error returns a concatenated string of the contained errors. |
| 86 | func (e multiError) Error() string { |
| 87 | var buf bytes.Buffer |
| 88 | |
| 89 | if len(e.errs) > 1 { |
| 90 | fmt.Fprintf(&buf, "%d errors: ", len(e.errs)) |
| 91 | } |
| 92 | for i, err := range e.errs { |
| 93 | if i != 0 { |
| 94 | buf.WriteString("; ") |
| 95 | } |
| 96 | buf.WriteString(err.Error()) |
| 97 | } |
| 98 | |
| 99 | return buf.String() |
| 100 | } |
| 101 | |
| 102 | // As finds the first error in multiError slice of error chains that matches target, and if so, sets |
| 103 | // target to that error value and returns true. Otherwise, it returns false. |
| 104 | // |
| 105 | // An error matches target if the error's concrete value is assignable to the value |
| 106 | // pointed to by target, or if the error has a method As(interface{}) bool such that |
| 107 | // As(target) returns true. In the latter case, the As method is responsible for |
| 108 | // setting target. |
| 109 | func (e multiError) As(target interface{}) bool { |
| 110 | if t, ok := target.(*multiError); ok { |
| 111 | *t = e |
| 112 | return true |
| 113 | } |
| 114 | |
| 115 | for _, err := range e.errs { |
| 116 | if stderrors.As(err, target) { |
| 117 | return true |
| 118 | } |
| 119 | } |
| 120 | return false |
| 121 | } |
| 122 | |
| 123 | // Is returns true if any error in multiError's slice of error chains matches the given target or |
| 124 | // if the target is of multiError type. |
| 125 | // |
| 126 | // An error is considered to match a target if it is equal to that target or if |
| 127 | // it implements a method Is(error) bool such that Is(target) returns true. |
| 128 | func (e multiError) Is(target error) bool { |
| 129 | if m, ok := target.(multiError); ok { |
| 130 | if len(m.errs) != len(e.errs) { |
| 131 | return false |
| 132 | } |
| 133 | for i := 0; i < len(e.errs); i++ { |
| 134 | if !stderrors.Is(m.errs[i], e.errs[i]) { |
| 135 | return false |
| 136 | } |
| 137 | } |
| 138 | return true |
| 139 | } |
| 140 | for _, err := range e.errs { |
| 141 | if stderrors.Is(err, target) { |
| 142 | return true |
| 143 | } |
| 144 | } |
| 145 | return false |
| 146 | } |
| 147 | |
| 148 | // Count returns the number of all multi error' errors that match the given target (including nested multi errors). |
| 149 | // Matching is defined as in Is method. |
| 150 | func (e multiError) Count(target error) (count int) { |
| 151 | for _, err := range e.errs { |
| 152 | if inner, ok := AsMulti(err); ok { |
| 153 | count += inner.Count(target) |
| 154 | continue |
| 155 | } |
| 156 | |
| 157 | if stderrors.Is(err, target) { |
| 158 | count++ |
| 159 | } |
| 160 | } |
| 161 | return count |
| 162 | } |
| 163 | |
| 164 | // AsMulti casts error to multi error read only interface. It returns multi error and true if error matches multi error as |
| 165 | // defined by As method. If returns false if no multi error can be found. |
| 166 | func AsMulti(err error) (Error, bool) { |
| 167 | m := multiError{} |
| 168 | if !stderrors.As(err, &m) { |
| 169 | return nil, false |
| 170 | } |
| 171 | return m, true |
| 172 | } |
| 173 | |
| 174 | // Merge merges multiple Error to single one, but joining all errors together. |
| 175 | // NOTE: Nested multi errors are not merged. |
| 176 | func Merge(errs []Error) Error { |
| 177 | e := multiError{} |
| 178 | for _, err := range errs { |
| 179 | e.errs = append(e.errs, err.Errors()...) |
| 180 | } |
| 181 | return e |
| 182 | } |
| 183 | |
| 184 | // PrettyPrint prints the same information as multiError.Error() method but with newlines and indentation targeted |
| 185 | // for humans. |
| 186 | func PrettyPrint(w io.Writer, err Error) error { |
| 187 | return prettyPrint(w, "\t", err) |
| 188 | } |
| 189 | |
| 190 | func prettyPrint(w io.Writer, indent string, merr Error) error { |
| 191 | if len(merr.Errors()) > 1 { |
| 192 | if _, err := fmt.Fprintf(w, "%d errors:\n", len(merr.Errors())); err != nil { |
| 193 | return err |
| 194 | } |
| 195 | } |
| 196 | for i, err := range merr.Errors() { |
| 197 | if i != 0 { |
| 198 | if _, err := w.Write([]byte("\n")); err != nil { |
| 199 | return err |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if merr2, ok := AsMulti(err); ok { |
| 204 | if err := prettyPrint(w, indent+"\t", merr2); err != nil { |
| 205 | return nil |
| 206 | } |
| 207 | continue |
| 208 | } |
| 209 | |
| 210 | if _, err := w.Write([]byte(indent + err.Error())); err != nil { |
| 211 | return err |
| 212 | } |
| 213 | } |
| 214 | return nil |
| 215 | } |