@@ -24,6 +24,7 @@ import (
2424	"context" 
2525	"encoding/json" 
2626	"fmt" 
27+ 	"net/http" 
2728	"net/url" 
2829	"strconv" 
2930	"strings" 
@@ -167,7 +168,7 @@ func (r *Client) getRawDashboard(ctx context.Context, path string) ([]byte, Boar
167168	if  raw , code , err  =  r .get (ctx , fmt .Sprintf ("api/dashboards/%s" , path ), nil ); err  !=  nil  {
168169		return  nil , BoardProperties {}, err 
169170	}
170- 	if  code  !=  200  {
171+ 	if  code  !=  http . StatusOK  {
171172		return  nil , BoardProperties {}, fmt .Errorf ("HTTP error %d: returns %s" , code , raw )
172173	}
173174	dec  :=  json .NewDecoder (bytes .NewReader (raw ))
@@ -251,7 +252,7 @@ func (r *Client) Search(ctx context.Context, params ...SearchParam) ([]FoundBoar
251252	if  raw , code , err  =  r .get (ctx , "api/search" , q ); err  !=  nil  {
252253		return  nil , err 
253254	}
254- 	if  code  !=  200  {
255+ 	if  code  !=  http . StatusOK  {
255256		return  nil , fmt .Errorf ("HTTP error %d: returns %s" , code , raw )
256257	}
257258	err  =  json .Unmarshal (raw , & boards )
@@ -302,13 +303,20 @@ func (r *Client) SetDashboard(ctx context.Context, board Board, params SetDashbo
302303	if  raw , code , err  =  r .post (ctx , "api/dashboards/db" , nil , raw ); err  !=  nil  {
303304		return  StatusMessage {}, err 
304305	}
305- 	if  err  =  json .Unmarshal (raw , & resp ); err  !=  nil  {
306- 		return  StatusMessage {}, err 
307- 	}
308- 	if  code  !=  200  {
309- 		return  resp , fmt .Errorf ("HTTP error %d: returns %s" , code , * resp .Message )
306+ 	switch  code  { // https://grafana.com/docs/grafana/latest/http_api/dashboard/#create--update-dashboard 
307+ 	case  http .StatusOK :
308+ 		err  =  json .Unmarshal (raw , & resp )
309+ 	case  http .StatusForbidden :
310+ 		err  =  fmt .Errorf ("database dashboard with uid %q %w" , board .UID , ErrNotAccessDenied )
311+ 	case  http .StatusUnauthorized :
312+ 		err  =  fmt .Errorf ("database dashboard with uid %q %w" , board .UID , ErrNotAuthorized )
313+ 	case  http .StatusPreconditionFailed :
314+ 		err  =  fmt .Errorf ("database dashboard with uid %q %w" , board .UID , ErrCannotCreate )
315+ 	default : // includes http.StatusBadRequest 
316+ 		err  =  fmt .Errorf ("HTTP error %d: returns %s" , code , raw )
310317	}
311- 	return  resp , nil 
318+ 
319+ 	return  resp , err 
312320}
313321
314322//SetRawDashboardWithParam sends the serialized along with request parameters 
@@ -330,7 +338,7 @@ func (r *Client) SetRawDashboardWithParam(ctx context.Context, request RawBoardR
330338	if  err  =  json .Unmarshal (rawResp , & resp ); err  !=  nil  {
331339		return  StatusMessage {}, err 
332340	}
333- 	if  code  !=  200  {
341+ 	if  code  !=  http . StatusOK  {
334342		return  StatusMessage {}, fmt .Errorf ("HTTP error %d: returns %s" , code , * resp .Message )
335343	}
336344	return  resp , nil 
@@ -365,13 +373,17 @@ func (r *Client) DeleteDashboard(ctx context.Context, slug string) (StatusMessag
365373		raw            []byte 
366374		reply          StatusMessage 
367375		err            error 
376+ 		code           int 
368377	)
369378	if  slug , isBoardFromDB  =  cleanPrefix (slug ); ! isBoardFromDB  {
370379		return  StatusMessage {}, errors .New ("only database dashboards (with 'db/' prefix in a slug) can be removed" )
371380	}
372- 	if  raw , _ , err  =  r .delete (ctx , fmt .Sprintf ("api/dashboards/db/%s" , slug )); err  !=  nil  {
381+ 	if  raw , code , err  =  r .delete (ctx , fmt .Sprintf ("api/dashboards/db/%s" , slug )); err  !=  nil  {
373382		return  StatusMessage {}, err 
374383	}
384+ 	if  code  !=  http .StatusOK  {
385+ 		return  StatusMessage {}, fmt .Errorf ("HTTP error %d: returns %s" , code , raw )
386+ 	}
375387	err  =  json .Unmarshal (raw , & reply )
376388	return  reply , err 
377389}
@@ -383,10 +395,14 @@ func (r *Client) DeleteDashboardByUID(ctx context.Context, uid string) (StatusMe
383395		raw    []byte 
384396		reply  StatusMessage 
385397		err    error 
398+ 		code   int 
386399	)
387- 	if  raw , _ , err  =  r .delete (ctx , fmt .Sprintf ("api/dashboards/uid/%s" , uid )); err  !=  nil  {
400+ 	if  raw , code , err  =  r .delete (ctx , fmt .Sprintf ("api/dashboards/uid/%s" , uid )); err  !=  nil  {
388401		return  StatusMessage {}, err 
389402	}
403+ 	if  code  !=  http .StatusOK  {
404+ 		return  StatusMessage {}, fmt .Errorf ("HTTP error %d: returns %s" , code , raw )
405+ 	}
390406	err  =  json .Unmarshal (raw , & reply )
391407	return  reply , err 
392408}
0 commit comments