MapToError converts a JSON parsed map to an error type
(i map[string]interface{})
| 61 | |
| 62 | // MapToError converts a JSON parsed map to an error type |
| 63 | func MapToError(i map[string]interface{}) error { |
| 64 | msg, _ := i["message"].(string) |
| 65 | code, codeOK := i["code"].(int) |
| 66 | cause, causeOK := i["cause"].(map[string]interface{}) |
| 67 | |
| 68 | err := errors.New(msg) |
| 69 | |
| 70 | if codeOK { |
| 71 | err = &codedError{err, code} |
| 72 | } |
| 73 | |
| 74 | if causeOK { |
| 75 | causeError := MapToError(cause) |
| 76 | l := len(msg) - len(causeError.Error()) |
| 77 | msg = msg[:l-2] |
| 78 | err = &wrappedError{msg, causeError} |
| 79 | } |
| 80 | |
| 81 | return err |
| 82 | } |