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

Commit 393a4f4

Browse files
authored
Merge pull request #163 from grafana/err-not-found
Typed error for Not Found (404) responses
2 parents b3a3c20 + f575f36 commit 393a4f4

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

client.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,12 @@ 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+
BodyContents: bodyContents,
145+
}
146+
case resp.StatusCode >= 400:
142147
return fmt.Errorf("status: %d, body: %v", resp.StatusCode, string(bodyContents))
143148
}
144149

errors.go

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

0 commit comments

Comments
 (0)