ErrorToMap converts an error type to a key-value map
(err error, parent string)
| 40 | |
| 41 | // ErrorToMap converts an error type to a key-value map |
| 42 | func ErrorToMap(err error, parent string) map[string]interface{} { |
| 43 | data := make(map[string]interface{}) |
| 44 | if parent == err.Error() { |
| 45 | // NOTE: this is done because of how errors.Wrap works, internally, |
| 46 | // to build a stacktrace. We end up with duplicate |
| 47 | // entries in the tree of errors. |
| 48 | if c, ok := err.(causer); ok { |
| 49 | return ErrorToMap(c.Cause(), parent) |
| 50 | } |
| 51 | } |
| 52 | data["message"] = err.Error() |
| 53 | if c, ok := err.(coded); ok { |
| 54 | data["code"] = c.Code() |
| 55 | } |
| 56 | if c, ok := err.(causer); ok { |
| 57 | data["cause"] = ErrorToMap(c.Cause(), data["message"].(string)) |
| 58 | } |
| 59 | return data |
| 60 | } |
| 61 | |
| 62 | // MapToError converts a JSON parsed map to an error type |
| 63 | func MapToError(i map[string]interface{}) error { |