From a09b86e19a45fd51a8d8014412993b2d777c5fb3 Mon Sep 17 00:00:00 2001 From: Dimitrios Karagiannis Date: Fri, 11 Oct 2019 12:17:08 +0100 Subject: [PATCH] Improve megaport api error messages Signed-off-by: Dimitrios Karagiannis --- megaport/api/client.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/megaport/api/client.go b/megaport/api/client.go index c4431a6..4c0980f 100644 --- a/megaport/api/client.go +++ b/megaport/api/client.go @@ -186,7 +186,12 @@ func (c *Client) do(req *http.Request, data interface{}) error { if err := parseResponseBody(resp, &r); err != nil { return err } - return fmt.Errorf("megaport-api: %s: %w", r.Message, responseDataToError(r.Data)) + err := responseDataToError(r.Data) + if err != nil { + return fmt.Errorf("megaport-api: %s: %w", r.Message, err) + } else { + return fmt.Errorf("megaport-api: %s", r.Message) + } } return parseResponseBody(resp, &megaportResponse{Data: data}) } @@ -209,6 +214,8 @@ func responseDataToError(d interface{}) error { errors[i] = responseDataToError(v).Error() } return fmt.Errorf("%d errors: ['%s']", len(e), strings.Join(errors, "', '")) + case nil: + return nil default: return fmt.Errorf("cannot process error data of type %T: %#v", e, e) }