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

Commit

Permalink
Merge pull request #163 from grafana/err-not-found
Browse files Browse the repository at this point in the history
Typed error for Not Found (404) responses
  • Loading branch information
inkel authored Oct 4, 2023
2 parents b3a3c20 + f575f36 commit 393a4f4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,12 @@ 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{
BodyContents: bodyContents,
}
case resp.StatusCode >= 400:
return fmt.Errorf("status: %d, body: %v", resp.StatusCode, string(bodyContents))
}

Expand Down
11 changes: 11 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gapi

import "fmt"

type ErrNotFound struct {
BodyContents []byte
}

func (e ErrNotFound) Error() string {
return fmt.Sprintf("status: 404, body: %s", e.BodyContents)
}

0 comments on commit 393a4f4

Please sign in to comment.