Skip to content

Commit

Permalink
return the entire http response using a method
Browse files Browse the repository at this point in the history
  • Loading branch information
jooola committed Oct 19, 2023
1 parent 8de4cfa commit 5eae70c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion hcloud/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func errorFromResponse(resp *http.Response, body []byte) error {
}

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

Expand Down
4 changes: 2 additions & 2 deletions hcloud/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func TestClientError(t *testing.T) {
if apiError.Message != "An error occurred" {
t.Errorf("unexpected error message: %q", apiError.Message)
}
if apiError.HTTPStatusCode != http.StatusUnprocessableEntity {
t.Errorf("unexpected http status code: %q", apiError.HTTPStatusCode)
if apiError.Response().StatusCode != http.StatusUnprocessableEntity {
t.Errorf("unexpected http status code: %q", apiError.Response().StatusCode)
}
}

Expand Down
11 changes: 8 additions & 3 deletions hcloud/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package hcloud
import (
"fmt"
"net"
"net/http"
)

// ErrorCode represents an error code returned from the API.
Expand Down Expand Up @@ -94,15 +95,19 @@ type Error struct {
Code ErrorCode
Message string
Details interface{}
// HTTPStatusCode is the status code of the response that included the error.
// It can be converted to a user readable string using net/http.StatusText().
HTTPStatusCode int

response *http.Response
}

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

// Response returns the error underlying HTTP response.
func (e Error) Response() *http.Response {
return e.response
}

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

0 comments on commit 5eae70c

Please sign in to comment.