Skip to content

Commit 8910888

Browse files
authored
Remove deprecated experimental error source and fix some error sources (#423)
This PR removes usage of deprecated experimental error source package and fixes error source for `registry is not supported by GraphQL APIs` error as we are letting user know this is not working anymore.
1 parent f03d1f5 commit 8910888

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

pkg/dfutil/framer.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package dfutil
33
import (
44
"github.com/grafana/grafana-plugin-sdk-go/backend"
55
"github.com/grafana/grafana-plugin-sdk-go/data"
6-
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
76
)
87

98
// Framer is an interface that allows any type to be treated as a data frame
@@ -22,7 +21,10 @@ func FrameResponse(f Framer) backend.DataResponse {
2221
// This function is particularly useful if you have a function that returns `(Framer, error)`, which is a very common pattern
2322
func FrameResponseWithError(f Framer, err error) backend.DataResponse {
2423
if err != nil {
25-
res := errorsource.Response(err)
24+
if backend.IsDownstreamHTTPError(err) {
25+
err = backend.DownstreamError(err)
26+
}
27+
res := backend.ErrorResponseWithErrorSource(err)
2628
backend.Logger.Debug("Error response", "errorsource", res.ErrorSource, "error", res.Error)
2729
return res
2830
}

pkg/github/client/client.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
googlegithub "github.com/google/go-github/v53/github"
1414
"github.com/grafana/github-datasource/pkg/models"
1515
"github.com/grafana/grafana-plugin-sdk-go/backend"
16-
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
1716
"github.com/influxdata/tdigest"
1817
"github.com/shurcooL/githubv4"
1918
"golang.org/x/oauth2"
@@ -61,23 +60,23 @@ func New(ctx context.Context, settings models.Settings) (*Client, error) {
6160
return createAccessTokenClient(ctx, settings)
6261
}
6362

64-
return nil, errorsource.DownstreamError(fmt.Errorf("access token or app token are required"), false)
63+
return nil, backend.DownstreamError(errors.New("access token or app token are required"))
6564
}
6665

6766
func createAppClient(settings models.Settings) (*Client, error) {
6867
appId, err := strconv.ParseInt(settings.AppId, 10, 64)
6968
if err != nil {
70-
return nil, errorsource.DownstreamError(fmt.Errorf("error parsing app id"), false)
69+
return nil, backend.DownstreamError(errors.New("error parsing app id"))
7170
}
7271

7372
installationId, err := strconv.ParseInt(settings.InstallationId, 10, 64)
7473
if err != nil {
75-
return nil, errorsource.DownstreamError(fmt.Errorf("error parsing installation id"), false)
74+
return nil, backend.DownstreamError(errors.New("error parsing installation id"))
7675
}
7776

7877
itr, err := ghinstallation.New(http.DefaultTransport, appId, installationId, []byte(settings.PrivateKey))
7978
if err != nil {
80-
return nil, errorsource.DownstreamError(fmt.Errorf("error creating token source"), false)
79+
return nil, backend.DownstreamError(errors.New("error creating token source"))
8180
}
8281

8382
httpClient := &http.Client{Transport: itr}
@@ -112,12 +111,12 @@ func createAccessTokenClient(ctx context.Context, settings models.Settings) (*Cl
112111
func useGitHubEnterprise(httpClient *http.Client, settings models.Settings) (*Client, error) {
113112
_, err := url.Parse(settings.GitHubURL)
114113
if err != nil {
115-
return nil, errorsource.DownstreamError(fmt.Errorf("incorrect enterprise url"), false)
114+
return nil, backend.DownstreamError(errors.New("incorrect enterprise url"))
116115
}
117116

118117
restClient, err := googlegithub.NewEnterpriseClient(settings.GitHubURL, settings.GitHubURL, httpClient)
119118
if err != nil {
120-
return nil, fmt.Errorf("instantiating enterprise rest client: %w", err)
119+
return nil, backend.DownstreamError(errors.New("instantiating enterprise rest client"))
121120
}
122121

123122
return &Client{
@@ -227,7 +226,7 @@ func (client *Client) GetWorkflowUsage(ctx context.Context, owner, repo, workflo
227226
usage, response, err := client.getWorkflowUsage(ctx, owner, repo, workflow)
228227
if err != nil {
229228
if response.StatusCode == http.StatusNotFound {
230-
return models.WorkflowUsage{}, errorsource.DownstreamError(errWorkflowNotFound, false)
229+
return models.WorkflowUsage{}, backend.DownstreamError(errWorkflowNotFound)
231230
}
232231
return models.WorkflowUsage{}, addErrorSourceToError(fmt.Errorf("fetching workflow usage: %w", err), response)
233232
}
@@ -333,7 +332,7 @@ func (client *Client) getWorkflowRuns(ctx context.Context, owner, repo, workflow
333332
if err != nil {
334333
// If the workflow is not found, return a specific error.
335334
if response != nil && response.StatusCode == http.StatusNotFound {
336-
return nil, 0, errorsource.SourceError(backend.ErrorSourceDownstream, errWorkflowNotFound, false)
335+
return nil, 0, backend.DownstreamError(errWorkflowNotFound)
337336
}
338337
return nil, 0, addErrorSourceToError(fmt.Errorf("fetching workflow runs: %w", err), response)
339338
}

pkg/github/client/errorsourcehandling.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package githubclient
22

33
import (
4-
"context"
54
"errors"
65
"regexp"
76
"strconv"
87
"strings"
9-
"syscall"
108

119
googlegithub "github.com/google/go-github/v53/github"
1210
"github.com/grafana/grafana-plugin-sdk-go/backend"
13-
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
1411
)
1512

1613
var statusErrorStringFromGraphQLPackage = "non-200 OK status code: "
@@ -19,9 +16,11 @@ var (
1916
downstreamErrors = []string{
2017
"Could not resolve to",
2118
"Your token has not been granted the required scopes to execute this query",
22-
"Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization.",
19+
"Resource protected by organization SAML enforcement",
20+
"Resource not accessible by personal access token",
2321
"API rate limit exceeded",
2422
"Resource not accessible by integration", // issue with incorrectly set permissions for token/app
23+
"registry is not supported by GraphQL APIs",
2524
}
2625
)
2726

@@ -31,13 +30,13 @@ func addErrorSourceToError(err error, resp *googlegithub.Response) error {
3130
return nil
3231
}
3332

34-
if errors.Is(err, syscall.ECONNREFUSED) || errors.Is(err, context.Canceled) {
35-
return errorsource.DownstreamError(err, false)
33+
if backend.IsDownstreamHTTPError(err) {
34+
return backend.DownstreamError(err)
3635
}
3736

3837
for _, downstreamError := range downstreamErrors {
3938
if strings.Contains(err.Error(), downstreamError) {
40-
return errorsource.DownstreamError(err, false)
39+
return backend.DownstreamError(err)
4140
}
4241
}
4342
// Unfortunately graphql library that is used is not returning original error from the client.
@@ -46,13 +45,19 @@ func addErrorSourceToError(err error, resp *googlegithub.Response) error {
4645
if strings.Contains(err.Error(), statusErrorStringFromGraphQLPackage) {
4746
statusCode, statusErr := extractStatusCode(err)
4847
if statusErr == nil {
49-
return errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(statusCode), err, false)
48+
if backend.ErrorSourceFromHTTPStatus(statusCode) == backend.ErrorSourceDownstream {
49+
return backend.DownstreamError(err)
50+
}
51+
return backend.PluginError(err)
5052
}
5153
}
5254
// If we have response we can use the status code from it
5355
if resp != nil {
5456
if resp.StatusCode/100 != 2 {
55-
return errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(resp.StatusCode), err, false)
57+
if backend.ErrorSourceFromHTTPStatus(resp.StatusCode) == backend.ErrorSourceDownstream {
58+
return backend.DownstreamError(err)
59+
}
60+
return backend.PluginError(err)
5661
}
5762
}
5863
// Otherwise we are not adding source which means it is going to be plugin error

pkg/github/client/errorsourcehandling_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package githubclient
33
import (
44
"context"
55
"errors"
6+
"net"
67
"net/http"
8+
"os"
79
"syscall"
810
"testing"
911

1012
googlegithub "github.com/google/go-github/v53/github"
1113
"github.com/grafana/grafana-plugin-sdk-go/backend"
12-
"github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
1314
"github.com/stretchr/testify/require"
1415
)
1516

@@ -28,27 +29,33 @@ func TestAddErrorSourceToError(t *testing.T) {
2829
},
2930
{
3031
name: "ECONNREFUSED error",
31-
err: syscall.ECONNREFUSED,
32+
err: &net.OpError{Err: &os.SyscallError{Err: syscall.ECONNREFUSED}},
3233
resp: nil,
33-
expected: errorsource.DownstreamError(syscall.ECONNREFUSED, false),
34+
expected: backend.DownstreamError(&net.OpError{Err: &os.SyscallError{Err: syscall.ECONNREFUSED}}),
35+
},
36+
{
37+
name: "DNS not found error",
38+
err: &net.DNSError{IsNotFound: true},
39+
resp: nil,
40+
expected: backend.DownstreamError(&net.DNSError{IsNotFound: true}),
3441
},
3542
{
3643
name: "graphql error with status code",
3744
err: errors.New("non-200 OK status code: 404 Not Found"),
3845
resp: nil,
39-
expected: errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(404), errors.New("non-200 OK status code: 404 Not Found"), false),
46+
expected: backend.DownstreamError(errors.New("non-200 OK status code: 404 Not Found")),
4047
},
4148
{
4249
name: "identified downstream graphql error",
4350
err: errors.New("Your token has not been granted the required scopes to execute this query"),
4451
resp: nil,
45-
expected: errorsource.DownstreamError(errors.New("Your token has not been granted the required scopes to execute this query"),false),
52+
expected: backend.DownstreamError(errors.New("Your token has not been granted the required scopes to execute this query")),
4653
},
4754
{
4855
name: "response with non-2xx status code",
4956
err: errors.New("some other error"),
5057
resp: &googlegithub.Response{Response: &http.Response{StatusCode: 500}},
51-
expected: errorsource.SourceError(backend.ErrorSourceFromHTTPStatus(500), errors.New("some other error"), false),
58+
expected: backend.DownstreamError(errors.New("some other error")),
5259
},
5360
{
5461
name: "other error with 2xx status code",
@@ -60,25 +67,25 @@ func TestAddErrorSourceToError(t *testing.T) {
6067
name: "context canceled error",
6168
err: context.Canceled,
6269
resp: nil,
63-
expected: errorsource.DownstreamError(context.Canceled, false),
70+
expected: backend.DownstreamError(context.Canceled),
6471
},
6572
{
6673
name: "saml error message",
6774
err: errors.New("Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization."),
6875
resp: nil,
69-
expected: errorsource.DownstreamError(errors.New("Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization."), false),
76+
expected: backend.DownstreamError(errors.New("Resource protected by organization SAML enforcement. You must grant your Personal Access token access to this organization.")),
7077
},
7178
{
7279
name: "limit exceeded error message",
7380
err: errors.New("API rate limit exceeded for ID 1"),
7481
resp: nil,
75-
expected: errorsource.DownstreamError(errors.New("API rate limit exceeded for ID 1"), false),
82+
expected: backend.DownstreamError(errors.New("API rate limit exceeded for ID 1")),
7683
},
7784
{
7885
name: "permission error message",
7986
err: errors.New("Resource not accessible by integration"),
8087
resp: nil,
81-
expected: errorsource.DownstreamError(errors.New("Resource not accessible by integration"), false),
88+
expected: backend.DownstreamError(errors.New("Resource not accessible by integration")),
8289
},
8390
}
8491

pkg/github/query_handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func UnmarshalQuery(b []byte, v interface{}) *backend.DataResponse {
3232
if err := json.Unmarshal(b, v); err != nil {
3333
return &backend.DataResponse{
3434
Error: errors.Wrap(err, "failed to unmarshal JSON request into query"),
35+
ErrorSource: backend.ErrorSourceDownstream,
3536
}
3637
}
3738
return nil

0 commit comments

Comments
 (0)