Skip to content

Commit

Permalink
Merge pull request #136 from K-Phoen/delete-dashboard-404
Browse files Browse the repository at this point in the history
Return specific error on dashboard not found during delete
  • Loading branch information
K-Phoen authored Nov 27, 2021
2 parents 93ddb0b + 67b34ad commit 8643e51
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
// ErrFolderNotFound is returned when the given folder can not be found.
var ErrFolderNotFound = errors.New("folder not found")

// ErrDashboardNotFound is returned when the given dashboard can not be found.
var ErrDashboardNotFound = errors.New("dashboard not found")

// ErrAlertChannelNotFound is returned when the given alert notification
// channel can not be found.
var ErrAlertChannelNotFound = errors.New("alert channel not found")
Expand Down Expand Up @@ -252,6 +255,9 @@ func (client *Client) DeleteDashboard(ctx context.Context, uid string) error {

defer func() { _ = resp.Body.Close() }()

if resp.StatusCode == http.StatusNotFound {
return ErrDashboardNotFound
}
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,18 @@ func TestDeleteDashboardCanFail(t *testing.T) {

req.Error(err)
}

func TestDeletingANonExistingDashboardReturnsSpecificError(t *testing.T) {
req := require.New(t)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintln(w, `{"message": "oh noes, does not exist"}`)
}))
defer ts.Close()

client := NewClient(http.DefaultClient, ts.URL)

err := client.DeleteDashboard(context.TODO(), "some uid")

req.Equal(ErrDashboardNotFound, err)
}

0 comments on commit 8643e51

Please sign in to comment.