Encodes the given error into fields of an object. A field with the given name is added for the error message. If the error implements fmt.Formatter, a field with the name ${key}Verbose is also added with the full verbose error message. Finally, if the error implements errorGroup (from go.uber.org/
(key string, err error, enc ObjectEncoder)
| 45 | // ], |
| 46 | // } |
| 47 | func encodeError(key string, err error, enc ObjectEncoder) (retErr error) { |
| 48 | // Try to capture panics (from nil references or otherwise) when calling |
| 49 | // the Error() method |
| 50 | defer func() { |
| 51 | if rerr := recover(); rerr != nil { |
| 52 | // If it's a nil pointer, just say "<nil>". The likeliest causes are a |
| 53 | // error that fails to guard against nil or a nil pointer for a |
| 54 | // value receiver, and in either case, "<nil>" is a nice result. |
| 55 | if v := reflect.ValueOf(err); v.Kind() == reflect.Ptr && v.IsNil() { |
| 56 | enc.AddString(key, "<nil>") |
| 57 | return |
| 58 | } |
| 59 | |
| 60 | retErr = fmt.Errorf("PANIC=%v", rerr) |
| 61 | } |
| 62 | }() |
| 63 | |
| 64 | basic := err.Error() |
| 65 | enc.AddString(key, basic) |
| 66 | |
| 67 | switch e := err.(type) { |
| 68 | case errorGroup: |
| 69 | return enc.AddArray(key+"Causes", errArray(e.Errors())) |
| 70 | case fmt.Formatter: |
| 71 | verbose := fmt.Sprintf("%+v", e) |
| 72 | if verbose != basic { |
| 73 | // This is a rich error type, like those produced by |
| 74 | // github.com/pkg/errors. |
| 75 | enc.AddString(key+"Verbose", verbose) |
| 76 | } |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | type errorGroup interface { |
| 82 | // Provides read-only access to the underlying list of errors, preferably |