diff --git a/ovh/ovh.go b/ovh/ovh.go index 737c9447..c5f7f726 100644 --- a/ovh/ovh.go +++ b/ovh/ovh.go @@ -188,9 +188,10 @@ func (c *Client) getResponse(response *http.Response, resType interface{}) error if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusMultipleChoices { apiError := &APIError{Code: response.StatusCode} if err = json.Unmarshal(body, apiError); err != nil { - return err + apiError.Message = string(body) } apiError.QueryID = response.Header.Get("X-Ovh-QueryID") + return apiError } diff --git a/ovh/ovh_test.go b/ovh/ovh_test.go index 19050233..af2aa6d9 100644 --- a/ovh/ovh_test.go +++ b/ovh/ovh_test.go @@ -61,7 +61,7 @@ func initMockServer(InputRequest **http.Request, status int, responseBody string // Respond w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) - fmt.Fprintln(w, responseBody) + fmt.Fprint(w, responseBody) })) // Create client @@ -131,6 +131,30 @@ func TestPing(t *testing.T) { } } +func TestError500HTML(t *testing.T) { + // Init test + var InputRequest *http.Request + errHTML := `

test

` + ts, client := initMockServer(&InputRequest, http.StatusServiceUnavailable, errHTML, nil) + defer ts.Close() + + // Test + var res struct{} + err := client.CallAPI("GET", "/test", nil, &res, false) + + // Validate + if err == nil { + t.Fatal("Expected error") + } + apiError := &APIError{ + Code: http.StatusServiceUnavailable, + Message: errHTML, + } + if err.Error() != apiError.Error() { + t.Fatalf("Missmatch errors : \n%s\n%s", err, apiError) + } +} + func TestPingUnreachable(t *testing.T) { // Init test var InputRequest *http.Request