Skip to content

Commit 5301079

Browse files
committed
Return HTTP response codes as typed errors
This allows to differentiate a dashboard not found from other errors, e.g. ```go _, err = client.GetDatasource(ctx, dsID) if errors.Is(err, sdk.ErrNotFound) { fmt.Fprintf(os.Stderr, "Creating new datasource %s (id=%d)\n", ds.Name, ds.ID) } ```
1 parent 11b1efc commit 5301079

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

rest-datasource.go

+36-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@ package sdk
2222
import (
2323
"context"
2424
"encoding/json"
25+
"errors"
2526
"fmt"
27+
"net/http"
28+
)
29+
30+
var (
31+
ErrNotFound = errors.New("not found")
32+
ErrAlreadyExists = errors.New("already exists")
2633
)
2734

2835
// GetAllDatasources gets all datasources.
@@ -56,10 +63,14 @@ func (r *Client) GetDatasource(ctx context.Context, id uint) (Datasource, error)
5663
if raw, code, err = r.get(ctx, fmt.Sprintf("api/datasources/%d", id), nil); err != nil {
5764
return ds, err
5865
}
59-
if code != 200 {
60-
return ds, fmt.Errorf("HTTP error %d: returns %s", code, raw)
66+
switch code {
67+
case http.StatusOK:
68+
err = json.Unmarshal(raw, &ds)
69+
case http.StatusNotFound:
70+
err = fmt.Errorf("data source with id %q %w", id, ErrNotFound)
71+
default:
72+
err = fmt.Errorf("HTTP error %d: returns %s", code, raw)
6173
}
62-
err = json.Unmarshal(raw, &ds)
6374
return ds, err
6475
}
6576

@@ -89,17 +100,23 @@ func (r *Client) CreateDatasource(ctx context.Context, ds Datasource) (StatusMes
89100
raw []byte
90101
resp StatusMessage
91102
err error
103+
code int
92104
)
93105
if raw, err = json.Marshal(ds); err != nil {
94106
return StatusMessage{}, err
95107
}
96-
if raw, _, err = r.post(ctx, "api/datasources", nil, raw); err != nil {
108+
if raw, code, err = r.post(ctx, "api/datasources", nil, raw); err != nil {
97109
return StatusMessage{}, err
98110
}
99-
if err = json.Unmarshal(raw, &resp); err != nil {
100-
return StatusMessage{}, err
111+
switch code {
112+
case http.StatusOK:
113+
err = json.Unmarshal(raw, &resp)
114+
case http.StatusConflict:
115+
err = fmt.Errorf("data source with name %q %w", ds.Name, ErrAlreadyExists)
116+
default:
117+
err = fmt.Errorf("HTTP status code %d: returns %s", code, raw)
101118
}
102-
return resp, nil
119+
return resp, err
103120
}
104121

105122
// UpdateDatasource updates a datasource from data passed in argument.
@@ -109,17 +126,23 @@ func (r *Client) UpdateDatasource(ctx context.Context, ds Datasource) (StatusMes
109126
raw []byte
110127
resp StatusMessage
111128
err error
129+
code int
112130
)
113131
if raw, err = json.Marshal(ds); err != nil {
114-
return StatusMessage{}, err
132+
return resp, err
115133
}
116-
if raw, _, err = r.put(ctx, fmt.Sprintf("api/datasources/%d", ds.ID), nil, raw); err != nil {
117-
return StatusMessage{}, err
134+
if raw, code, err = r.put(ctx, fmt.Sprintf("api/datasources/%d", ds.ID), nil, raw); err != nil {
135+
return resp, err
118136
}
119-
if err = json.Unmarshal(raw, &resp); err != nil {
120-
return StatusMessage{}, err
137+
switch code {
138+
case http.StatusOK:
139+
err = json.Unmarshal(raw, &resp)
140+
case http.StatusNotFound:
141+
err = fmt.Errorf("data source with name %q %w", ds.Name, ErrNotFound)
142+
default:
143+
err = fmt.Errorf("HTTP status code %d: returns %s", code, raw)
121144
}
122-
return resp, nil
145+
return resp, err
123146
}
124147

125148
// DeleteDatasource deletes an existing datasource by ID.

0 commit comments

Comments
 (0)