sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Return HTTP response codes as typed errors

Open suhlig opened this issue 4 years ago • 11 comments
trafficstars

This allows to differentiate a datasource not found from other errors, e.g.

_, 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)
}

suhlig avatar Aug 07 '21 16:08 suhlig

Thanks for the feedback. I'll be back with some updates to this PR.

suhlig avatar Aug 10 '21 11:08 suhlig

I amended my commit with the changes you proposed. Test are green on my workstation; not sure how I could run the GitHub actions.

suhlig avatar Aug 15 '21 11:08 suhlig

It seems like linters are failing. Could you please take a look?

GiedriusS avatar Aug 16 '21 19:08 GiedriusS

Should be fixed now.

suhlig avatar Aug 17 '21 17:08 suhlig

Still, some errors are happening. AFAICT the issue is here: https://github.com/grafana-tools/sdk/blob/master/rest-dashboard_integration_test.go#L87-L90

We should ignore 404 errors here (: @suhlig could you please take a look?

GiedriusS avatar Aug 18 '21 15:08 GiedriusS

Sure! Thanks for catching these. I ran go test ./...; shouldn't that have shown the error?

suhlig avatar Aug 18 '21 15:08 suhlig

We also have integration tests. For that, you need a locally running Grafana, most likely in a Docker container, plus environment variable GRAFANA_INTEGRATION set to 1. You can see how it works here.

GiedriusS avatar Aug 18 '21 15:08 GiedriusS

The integration test failure turned out to be a valuable one. I had overlooked a few error cases for delete-dashboard-by-uid and get-dashboard-by-uid.

I changed the integration test to consistently use DeleteDashboardByUID because this one has the 404 documented, whereas delete-dashboard-by-slug (DELETE /api/dashboards/db/:slug) as used by DeleteDashboard is deprecated since Grafana 5.0.

I also added instructions to the README on how to run local tests.

suhlig avatar Aug 19 '21 21:08 suhlig

Coverage Status

Coverage decreased (-1.7%) to 46.252% when pulling 66a7bce801909608578909e656e358ec54dc356e on suhlig:datasources-use-status-code into 9de4d14888c553cbde643c3c9fd26633a13d5736 on grafana-tools:master.

coveralls avatar Aug 20 '21 15:08 coveralls

Thanks, I will take a look tomorrow

GiedriusS avatar Aug 23 '21 16:08 GiedriusS

Bump ... anything I need to do before the PR can be merged?

suhlig avatar Nov 03 '21 11:11 suhlig