Skip to content

Commit

Permalink
refactor: use hcloud-go Response
Browse files Browse the repository at this point in the history
  • Loading branch information
apricote committed Oct 20, 2023
1 parent 5eae70c commit 908758a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
6 changes: 3 additions & 3 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 Down
7 changes: 3 additions & 4 deletions hcloud/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package hcloud
import (
"fmt"
"net"
"net/http"
)

// ErrorCode represents an error code returned from the API.
Expand Down Expand Up @@ -96,15 +95,15 @@ type Error struct {
Message string
Details interface{}

response *http.Response
response *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 {
// Response returns the [Response] that contained the error if available.
func (e Error) Response() *Response {
return e.response
}

Expand Down

0 comments on commit 908758a

Please sign in to comment.