Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit 7779aab

Browse files
committed
Typed error for Not Found (404) responses
1 parent b3a3c20 commit 7779aab

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ func (c *Client) request(method, requestPath string, query url.Values, body []by
138138
}
139139

140140
// check status code.
141-
if resp.StatusCode >= 400 {
141+
switch {
142+
case resp.StatusCode == http.StatusNotFound:
143+
return ErrNotFound{
144+
StatusCode: resp.StatusCode,
145+
BodyContents: bodyContents,
146+
}
147+
case resp.StatusCode >= 400:
142148
return fmt.Errorf("status: %d, body: %v", resp.StatusCode, string(bodyContents))
143149
}
144150

errors.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package gapi
2+
3+
import "fmt"
4+
5+
type ErrNotFound struct {
6+
StatusCode int
7+
BodyContents []byte
8+
}
9+
10+
func (e ErrNotFound) Error() string {
11+
return fmt.Sprintf("status: %d, body: %v", e.StatusCode, string(e.BodyContents))
12+
}

0 commit comments

Comments
 (0)