diff --git a/ovh/error.go b/ovh/error.go index 0aa54d27..35e3608e 100644 --- a/ovh/error.go +++ b/ovh/error.go @@ -8,6 +8,8 @@ type APIError struct { Message string // HTTP code. Code int + // ID of the request + QueryID string } func (err *APIError) Error() string { diff --git a/ovh/ovh.go b/ovh/ovh.go index 1e3309b1..737c9447 100644 --- a/ovh/ovh.go +++ b/ovh/ovh.go @@ -190,7 +190,7 @@ func (c *Client) getResponse(response *http.Response, resType interface{}) error if err = json.Unmarshal(body, apiError); err != nil { return err } - + apiError.QueryID = response.Header.Get("X-Ovh-QueryID") return apiError } diff --git a/ovh/ovh_test.go b/ovh/ovh_test.go index aa9ecfa3..19050233 100644 --- a/ovh/ovh_test.go +++ b/ovh/ovh_test.go @@ -293,6 +293,21 @@ func TestGetResponse(t *testing.T) { t.Fatalf("getResponse should return an error when failing to decode HTTP Response body. %v", err) } + // Error with QueryID + responseHeaders := http.Header{} + responseHeaders.Add("X-Ovh-QueryID", "FR.ws-8.5860f657.4632.0180") + err = mockClient.getResponse(&http.Response{ + StatusCode: 400, + Body: ioutil.NopCloser(strings.NewReader(`{"code": 400, "message": "Ooops..."}`)), + Header: responseHeaders, + }, &apiInt) + apiErr, ok := err.(*APIError) + if !ok { + t.Fatalf("Client.getResponse error should be an APIError when status is 400 and header QueryID is found. Got '%s' of type %s", err, reflect.TypeOf(err)) + } + if apiErr.QueryID != "FR.ws-8.5860f657.4632.0180" { + t.Fatalf("APIError should be filled with a correct QueryID. Got '%s' instead of '%s'", apiErr.QueryID, "FR.ws-8.5860f657.4632.0180") + } } func TestConstructors(t *testing.T) {