Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(error): include http response in api errors #320

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions hcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ func (c *Client) Do(r *http.Request, v interface{}) (*Response, error) {
return response, fmt.Errorf("hcloud: error reading response meta data: %s", err)
}

if resp.StatusCode >= 400 && resp.StatusCode <= 599 {
err = errorFromResponse(resp, body)
if response.StatusCode >= 400 && response.StatusCode <= 599 {
err = errorFromResponse(response, body)
if err == nil {
err = fmt.Errorf("hcloud: server responded with status code %d", resp.StatusCode)
} else if IsError(err, ErrorCodeConflict) {
Expand Down Expand Up @@ -359,7 +359,7 @@ func dumpRequest(r *http.Request) ([]byte, error) {
return dumpReq, nil
}

func errorFromResponse(resp *http.Response, body []byte) error {
func errorFromResponse(resp *Response, body []byte) error {
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
return nil
}
Expand All @@ -371,7 +371,10 @@ func errorFromResponse(resp *http.Response, body []byte) error {
if respBody.Error.Code == "" && respBody.Error.Message == "" {
return nil
}
return ErrorFromSchema(respBody.Error)

hcErr := ErrorFromSchema(respBody.Error)
hcErr.response = resp
return hcErr
}

// Response represents a response from the API. It embeds http.Response.
Expand Down
3 changes: 3 additions & 0 deletions hcloud/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ func TestClientError(t *testing.T) {
if apiError.Message != "An error occurred" {
t.Errorf("unexpected error message: %q", apiError.Message)
}
if apiError.Response().StatusCode != http.StatusUnprocessableEntity {
t.Errorf("unexpected http status code: %q", apiError.Response().StatusCode)
}
}

func TestClientInvalidToken(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions hcloud/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,19 @@ type Error struct {
Code ErrorCode
Message string
Details interface{}

response *Response
}

func (e Error) Error() string {
return fmt.Sprintf("%s (%s)", e.Message, e.Code)
}

// Response returns the [Response] that contained the error if available.
func (e Error) Response() *Response {
return e.response
}

// ErrorDetailsInvalidInput contains the details of an 'invalid_input' error.
type ErrorDetailsInvalidInput struct {
Fields []ErrorDetailsInvalidInputField
Expand Down
Loading