|
| 1 | +package commonerrors |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + TypeReasonErrorSeparator = ':' |
| 12 | + MultipleErrorSeparator = '\n' |
| 13 | +) |
| 14 | + |
| 15 | +type iMarshallingError interface { |
| 16 | + encoding.TextMarshaler |
| 17 | + encoding.TextUnmarshaler |
| 18 | + fmt.Stringer |
| 19 | + error |
| 20 | + ConvertToError() error |
| 21 | + SetWrappedError(err error) |
| 22 | +} |
| 23 | + |
| 24 | +type marshallingError struct { |
| 25 | + Reason string |
| 26 | + ErrorType error |
| 27 | +} |
| 28 | + |
| 29 | +func (e *marshallingError) MarshalText() (text []byte, err error) { |
| 30 | + str := e.String() |
| 31 | + return []byte(str), nil |
| 32 | +} |
| 33 | + |
| 34 | +func (e *marshallingError) String() string { |
| 35 | + return serialiseMarshallingError(e) |
| 36 | +} |
| 37 | + |
| 38 | +func (e *marshallingError) Error() string { |
| 39 | + return e.String() |
| 40 | +} |
| 41 | + |
| 42 | +func (e *marshallingError) UnmarshalText(text []byte) error { |
| 43 | + er := processErrorStrLine(string(text)) |
| 44 | + if er == nil { |
| 45 | + return ErrMarshalling |
| 46 | + } |
| 47 | + e.ErrorType = er.ErrorType |
| 48 | + e.Reason = er.Reason |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (e *marshallingError) ConvertToError() error { |
| 53 | + if e == nil { |
| 54 | + return nil |
| 55 | + } |
| 56 | + if e.ErrorType == nil { |
| 57 | + if e.Reason == "" { |
| 58 | + return nil |
| 59 | + } |
| 60 | + return errors.New(e.Reason) |
| 61 | + } |
| 62 | + if e.Reason == "" { |
| 63 | + return e.ErrorType |
| 64 | + } |
| 65 | + return fmt.Errorf("%w%v %v", e.ErrorType, string(TypeReasonErrorSeparator), e.Reason) |
| 66 | +} |
| 67 | + |
| 68 | +func (e *marshallingError) SetWrappedError(err error) { |
| 69 | + e.ErrorType = err |
| 70 | +} |
| 71 | + |
| 72 | +type multiplemarshallingError struct { |
| 73 | + subErrs []iMarshallingError |
| 74 | +} |
| 75 | + |
| 76 | +func (m *multiplemarshallingError) MarshalText() (text []byte, err error) { |
| 77 | + for i := range m.subErrs { |
| 78 | + subtext, suberr := m.subErrs[i].MarshalText() |
| 79 | + if suberr != nil { |
| 80 | + err = fmt.Errorf("%w%v an error item could not be marshalled%v %v", ErrMarshalling, string(TypeReasonErrorSeparator), string(TypeReasonErrorSeparator), suberr.Error()) |
| 81 | + return |
| 82 | + } |
| 83 | + text = append(text, subtext...) |
| 84 | + text = append(text, MultipleErrorSeparator) |
| 85 | + } |
| 86 | + return |
| 87 | +} |
| 88 | + |
| 89 | +func (m *multiplemarshallingError) String() string { |
| 90 | + text, err := m.MarshalText() |
| 91 | + if err == nil { |
| 92 | + return string(text) |
| 93 | + } |
| 94 | + return "" |
| 95 | +} |
| 96 | + |
| 97 | +func (m *multiplemarshallingError) Error() string { |
| 98 | + return m.String() |
| 99 | +} |
| 100 | + |
| 101 | +func (m *multiplemarshallingError) UnmarshalText(text []byte) error { |
| 102 | + sub := processErrorStr(string(text)) |
| 103 | + if IsEmpty(sub) { |
| 104 | + return ErrMarshalling |
| 105 | + } |
| 106 | + if mul, ok := sub.(*multiplemarshallingError); ok { |
| 107 | + m.subErrs = mul.subErrs |
| 108 | + } else { |
| 109 | + m.subErrs = append(m.subErrs, sub) |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func (m *multiplemarshallingError) ConvertToError() error { |
| 115 | + var errs []error |
| 116 | + for i := range m.subErrs { |
| 117 | + errs = append(errs, m.subErrs[i].ConvertToError()) |
| 118 | + } |
| 119 | + return errors.Join(errs...) |
| 120 | +} |
| 121 | + |
| 122 | +func (m *multiplemarshallingError) SetWrappedError(err error) { |
| 123 | + if err == nil { |
| 124 | + return |
| 125 | + } |
| 126 | + if x, ok := err.(interface{ Unwrap() []error }); ok { |
| 127 | + unwrapped := x.Unwrap() |
| 128 | + if len(unwrapped) > len(m.subErrs) { |
| 129 | + for i := 0; i < len(unwrapped)-len(m.subErrs); i++ { |
| 130 | + m.subErrs = append(m.subErrs, &marshallingError{}) |
| 131 | + } |
| 132 | + } |
| 133 | + for i := range unwrapped { |
| 134 | + subErr := m.subErrs[i] |
| 135 | + if subErr != nil { |
| 136 | + subErr.SetWrappedError(unwrapped[i]) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +func processErrorStr(s string) iMarshallingError { |
| 142 | + if strings.Contains(s, string(MultipleErrorSeparator)) { |
| 143 | + elems := strings.Split(s, string(MultipleErrorSeparator)) |
| 144 | + m := &multiplemarshallingError{} |
| 145 | + for i := range elems { |
| 146 | + mErr := processErrorStrLine(elems[i]) |
| 147 | + |
| 148 | + if mErr != nil { |
| 149 | + m.subErrs = append(m.subErrs, mErr) |
| 150 | + } |
| 151 | + } |
| 152 | + return m |
| 153 | + } else { |
| 154 | + return processErrorStrLine(s) |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +func processError(err error) (mErr iMarshallingError) { |
| 159 | + if err == nil { |
| 160 | + return |
| 161 | + } |
| 162 | + mErr = processErrorStr(err.Error()) |
| 163 | + if IsEmpty(mErr) { |
| 164 | + mErr = &marshallingError{ |
| 165 | + ErrorType: fmt.Errorf("%w%v error `%T` with no description returned", ErrUnknown, string(TypeReasonErrorSeparator), err), |
| 166 | + } |
| 167 | + return |
| 168 | + } |
| 169 | + switch x := err.(type) { |
| 170 | + case interface{ Unwrap() error }: |
| 171 | + mErr.SetWrappedError(x.Unwrap()) |
| 172 | + case interface{ Unwrap() []error }: |
| 173 | + unwrap := x.Unwrap() |
| 174 | + var nonNilUnwrappedErrors []error |
| 175 | + for i := range unwrap { |
| 176 | + if !IsEmpty(unwrap[i]) { |
| 177 | + nonNilUnwrappedErrors = append(nonNilUnwrappedErrors, unwrap[i]) |
| 178 | + } |
| 179 | + } |
| 180 | + mErr.SetWrappedError(errors.Join(nonNilUnwrappedErrors...)) |
| 181 | + } |
| 182 | + return |
| 183 | +} |
| 184 | + |
| 185 | +func processErrorStrLine(err string) (mErr *marshallingError) { |
| 186 | + err = strings.TrimSpace(err) |
| 187 | + if err == "" { |
| 188 | + return nil |
| 189 | + } |
| 190 | + mErr = &marshallingError{} |
| 191 | + elems := strings.Split(err, string(TypeReasonErrorSeparator)) |
| 192 | + found, commonErr := deserialiseCommonError(elems[0]) |
| 193 | + if !found || commonErr == nil { |
| 194 | + mErr.SetWrappedError(errors.New(strings.TrimSpace(elems[0]))) |
| 195 | + } else { |
| 196 | + mErr.SetWrappedError(commonErr) |
| 197 | + } |
| 198 | + if len(elems) > 0 { |
| 199 | + var reasonElems []string |
| 200 | + for i := 1; i < len(elems); i++ { |
| 201 | + reasonElems = append(reasonElems, strings.TrimSpace(elems[i])) |
| 202 | + } |
| 203 | + mErr.Reason = strings.Join(reasonElems, fmt.Sprintf("%v ", string(TypeReasonErrorSeparator))) |
| 204 | + } |
| 205 | + return |
| 206 | +} |
| 207 | + |
| 208 | +func serialiseMarshallingError(err *marshallingError) string { |
| 209 | + if err == nil { |
| 210 | + return "" |
| 211 | + } |
| 212 | + mErr := err.ConvertToError() |
| 213 | + if mErr == nil { |
| 214 | + return "" |
| 215 | + } |
| 216 | + return mErr.Error() |
| 217 | +} |
| 218 | + |
| 219 | +// SerialiseError marshals an error following a certain convention: `error type: reason`. |
| 220 | +func SerialiseError(err error) ([]byte, error) { |
| 221 | + mErr := processError(err) |
| 222 | + if mErr == nil { |
| 223 | + return nil, nil |
| 224 | + } |
| 225 | + return mErr.MarshalText() |
| 226 | +} |
| 227 | + |
| 228 | +// DeserialiseError unmarshals text into an error. It tries to determine the error type. |
| 229 | +func DeserialiseError(text []byte) (deserialisedError, err error) { |
| 230 | + if len(text) == 0 { |
| 231 | + return |
| 232 | + } |
| 233 | + var mErr iMarshallingError |
| 234 | + |
| 235 | + if strings.Contains(string(text), string(MultipleErrorSeparator)) { |
| 236 | + mErr = &multiplemarshallingError{} |
| 237 | + } else { |
| 238 | + mErr = &marshallingError{} |
| 239 | + } |
| 240 | + err = mErr.UnmarshalText(text) |
| 241 | + if err != nil { |
| 242 | + return |
| 243 | + } |
| 244 | + deserialisedError = mErr.ConvertToError() |
| 245 | + return |
| 246 | +} |
0 commit comments