From 7779aabd7b7809b2292a52d2cf0e0e8177495e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez=20de=20la=20Franca=20Beltran?= Date: Tue, 3 Oct 2023 17:53:28 +0200 Subject: [PATCH 1/2] Typed error for Not Found (404) responses --- client.go | 8 +++++++- errors.go | 12 ++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 errors.go diff --git a/client.go b/client.go index 4a19b0c0..360ebc81 100644 --- a/client.go +++ b/client.go @@ -138,7 +138,13 @@ func (c *Client) request(method, requestPath string, query url.Values, body []by } // check status code. - if resp.StatusCode >= 400 { + switch { + case resp.StatusCode == http.StatusNotFound: + return ErrNotFound{ + StatusCode: resp.StatusCode, + BodyContents: bodyContents, + } + case resp.StatusCode >= 400: return fmt.Errorf("status: %d, body: %v", resp.StatusCode, string(bodyContents)) } diff --git a/errors.go b/errors.go new file mode 100644 index 00000000..e2d349ca --- /dev/null +++ b/errors.go @@ -0,0 +1,12 @@ +package gapi + +import "fmt" + +type ErrNotFound struct { + StatusCode int + BodyContents []byte +} + +func (e ErrNotFound) Error() string { + return fmt.Sprintf("status: %d, body: %v", e.StatusCode, string(e.BodyContents)) +} From f575f36e365290e7fd6b89ea9a7961c276c89bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joan=20L=C3=B3pez=20de=20la=20Franca=20Beltran?= Date: Wed, 4 Oct 2023 10:52:10 +0200 Subject: [PATCH 2/2] Apply review feedback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Leandro López --- client.go | 1 - errors.go | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/client.go b/client.go index 360ebc81..17764f59 100644 --- a/client.go +++ b/client.go @@ -141,7 +141,6 @@ func (c *Client) request(method, requestPath string, query url.Values, body []by switch { case resp.StatusCode == http.StatusNotFound: return ErrNotFound{ - StatusCode: resp.StatusCode, BodyContents: bodyContents, } case resp.StatusCode >= 400: diff --git a/errors.go b/errors.go index e2d349ca..3b535d97 100644 --- a/errors.go +++ b/errors.go @@ -3,10 +3,9 @@ package gapi import "fmt" type ErrNotFound struct { - StatusCode int BodyContents []byte } func (e ErrNotFound) Error() string { - return fmt.Sprintf("status: %d, body: %v", e.StatusCode, string(e.BodyContents)) + return fmt.Sprintf("status: 404, body: %s", e.BodyContents) }