From 4dc6a6aca9492ddb7cc1b997d51dce3dc6fe64e8 Mon Sep 17 00:00:00 2001 From: codetruelle <130758140+codetruelle@users.noreply.github.com> Date: Fri, 14 Apr 2023 13:43:43 +0200 Subject: [PATCH 1/4] return status code if we cannot read the error body --- ovh/ovh.go | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/ovh/ovh.go b/ovh/ovh.go index 9c47e43..6ad609f 100644 --- a/ovh/ovh.go +++ b/ovh/ovh.go @@ -397,24 +397,36 @@ func (c *Client) CallAPIWithContext(ctx context.Context, method, path string, re // UnmarshalResponse checks the response and unmarshals it into the response // type if needed Helper function, called from CallAPI func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) error { - // Read all the response body - defer response.Body.Close() - body, err := ioutil.ReadAll(response.Body) - if err != nil { - return err - } // < 200 && >= 300 : API error if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices { + + // Read all the response body + defer response.Body.Close() + errBody, err := ioutil.ReadAll(response.Body) + if err != nil { + // If we cannot read the body, send back the status code as an error + apiError := &APIError{Code: response.StatusCode} + return apiError + } + + // if we have a body for the error - create API error from the body apiError := &APIError{Code: response.StatusCode} - if err = json.Unmarshal(body, apiError); err != nil { - apiError.Message = string(body) + if err = json.Unmarshal(errBody, apiError); err != nil { + apiError.Message = string(errBody) } apiError.QueryID = response.Header.Get("X-Ovh-QueryID") return apiError } + body, err := ioutil.ReadAll(response.Body) + if err != nil { + // If we cannot read the body, send back the status code as an error + apiError := &APIError{Code: response.StatusCode} + return apiError + } + // Nothing to unmarshal if len(body) == 0 || resType == nil { return nil @@ -424,3 +436,4 @@ func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) d.UseNumber() return d.Decode(&resType) } + From a9ae302373a934480fac7a93242434e9d5dfe521 Mon Sep 17 00:00:00 2001 From: codetruelle <130758140+codetruelle@users.noreply.github.com> Date: Fri, 14 Apr 2023 13:44:37 +0200 Subject: [PATCH 2/4] expect status code instead of body reader error --- ovh/ovh_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ovh/ovh_test.go b/ovh/ovh_test.go index 66bde9e..f27f163 100644 --- a/ovh/ovh_test.go +++ b/ovh/ovh_test.go @@ -325,11 +325,12 @@ func TestGetResponse(t *testing.T) { }, "Can parse a API error") td.Cmp(t, apiInt, 0) - // Error: body read error + // Error: Cannot read the error body err = client.UnmarshalResponse(&http.Response{ - Body: ErrorReadCloser{}, + StatusCode: 400, + Body: ErrorReadCloser{}, }, nil) - td.CmpString(t, err, "ErrorReader") + td.Cmp(t, err, &APIError{Code: 400}) // Error: HTTP Error + broken json err = client.UnmarshalResponse(&http.Response{ From 6e3fa8c17efeb78f31565891e9b8bba83a7f4089 Mon Sep 17 00:00:00 2001 From: codetruelle <130758140+codetruelle@users.noreply.github.com> Date: Fri, 14 Apr 2023 13:46:40 +0200 Subject: [PATCH 3/4] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index fb0de77..4831eab 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/ovh/go-ovh +module github.com/codetruelle/go-ovh go 1.18 From 5cc8b471ad6fb2380bb2d8e24646c9d35a4d06bf Mon Sep 17 00:00:00 2001 From: codetruelle <130758140+codetruelle@users.noreply.github.com> Date: Fri, 14 Apr 2023 14:34:24 +0200 Subject: [PATCH 4/4] Update go.mod --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 4831eab..fb0de77 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/codetruelle/go-ovh +module github.com/ovh/go-ovh go 1.18