Skip to content

Commit d99b10a

Browse files
authored
Error source (#99)
error source
1 parent 3159e66 commit d99b10a

File tree

6 files changed

+300
-515
lines changed

6 files changed

+300
-515
lines changed

datasource.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
const defaultKeySuffix = "default"
2121

2222
var (
23-
ErrorMissingMultipleConnectionsConfig = errors.New("received connection arguments but the feature is not enabled")
24-
ErrorMissingDBConnection = errors.New("unable to get default db connection")
23+
ErrorMissingMultipleConnectionsConfig = PluginError(errors.New("received connection arguments but the feature is not enabled"))
24+
ErrorMissingDBConnection = PluginError(errors.New("unable to get default db connection"))
2525
HeaderKey = "grafana-http-headers"
2626
// Deprecated: ErrorMissingMultipleConnectionsConfig should be used instead
2727
MissingMultipleConnectionsConfig = ErrorMissingMultipleConnectionsConfig
@@ -83,15 +83,15 @@ func getDatasourceUID(settings backend.DataSourceInstanceSettings) string {
8383
func (ds *SQLDatasource) NewDatasource(settings backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
8484
db, err := ds.c.Connect(settings, nil)
8585
if err != nil {
86-
return nil, err
86+
return nil, DownstreamError(err)
8787
}
8888
key := defaultKey(getDatasourceUID(settings))
8989
ds.storeDBConnection(key, dbConnection{db, settings})
9090

9191
mux := http.NewServeMux()
9292
err = ds.registerRoutes(mux)
9393
if err != nil {
94-
return nil, err
94+
return nil, PluginError(err)
9595
}
9696

9797
ds.CallResourceHandler = httpadapter.New(mux)
@@ -130,6 +130,9 @@ func (ds *SQLDatasource) QueryData(ctx context.Context, req *backend.QueryDataRe
130130
if err == nil {
131131
if responseMutator, ok := ds.c.(ResponseMutator); ok {
132132
frames, err = responseMutator.MutateResponse(ctx, frames)
133+
if err != nil {
134+
err = PluginError(err)
135+
}
133136
}
134137
}
135138
response.Set(query.RefID, backend.DataResponse{
@@ -178,7 +181,7 @@ func (ds *SQLDatasource) getDBConnectionFromQuery(q *Query, datasourceUID string
178181

179182
db, err := ds.c.Connect(dbConn.settings, q.ConnectionArgs)
180183
if err != nil {
181-
return "", dbConnection{}, err
184+
return "", dbConnection{}, DownstreamError(err)
182185
}
183186
// Assign this connection in the cache
184187
dbConn = dbConnection{db, dbConn.settings}
@@ -238,6 +241,8 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
238241
return res, nil
239242
}
240243

244+
// err = Unwrap(err)
245+
241246
if errors.Is(err, ErrorNoResults) {
242247
return res, nil
243248
}
@@ -251,7 +256,7 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
251256
backend.Logger.Warn(fmt.Sprintf("query failed: %s. Retrying %d times", err.Error(), i))
252257
db, err := ds.dbReconnect(dbConn, q, cacheKey)
253258
if err != nil {
254-
return nil, err
259+
return nil, DownstreamError(err)
255260
}
256261

257262
if ds.driverSettings.Pause > 0 {
@@ -295,7 +300,7 @@ func (ds *SQLDatasource) dbReconnect(dbConn dbConnection, q *Query, cacheKey str
295300

296301
db, err := ds.c.Connect(dbConn.settings, q.ConnectionArgs)
297302
if err != nil {
298-
return nil, err
303+
return nil, DownstreamError(err)
299304
}
300305
ds.storeDBConnection(cacheKey, dbConnection{db, dbConn.settings})
301306
return db, nil
@@ -306,7 +311,7 @@ func (ds *SQLDatasource) CheckHealth(ctx context.Context, req *backend.CheckHeal
306311
key := defaultKey(getDatasourceUID(*req.PluginContext.DataSourceInstanceSettings))
307312
dbConn, ok := ds.getDBConnection(key)
308313
if !ok {
309-
return nil, MissingDBConnection
314+
return nil, ErrorMissingDBConnection
310315
}
311316

312317
if ds.driverSettings.Retries == 0 {
@@ -361,7 +366,7 @@ func (ds *SQLDatasource) check(conn dbConnection) (*backend.CheckHealthResult, e
361366
return &backend.CheckHealthResult{
362367
Status: backend.HealthStatusError,
363368
Message: err.Error(),
364-
}, err
369+
}, DownstreamError(err)
365370
}
366371

367372
return &backend.CheckHealthResult{

errors.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package sqlds
22

3-
import "errors"
3+
import (
4+
"errors"
5+
6+
es "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
7+
)
48

59
var (
610
// ErrorBadDatasource is returned if the data source could not be asserted to the correct type (this should basically never happen?)
@@ -14,3 +18,11 @@ var (
1418
// ErrorNoResults is returned if there were no results returned
1519
ErrorNoResults = errors.New("no results returned from query")
1620
)
21+
22+
func PluginError(err error, override ...bool) error {
23+
return es.PluginError(err, len(override) > 0)
24+
}
25+
26+
func DownstreamError(err error, override ...bool) error {
27+
return es.DownstreamError(err, len(override) > 0)
28+
}

go.mod

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,77 +5,104 @@ go 1.20
55
require (
66
github.com/go-sql-driver/mysql v1.4.0
77
github.com/google/go-cmp v0.5.9
8-
github.com/grafana/grafana-plugin-sdk-go v0.159.0
8+
github.com/grafana/grafana-plugin-sdk-go v0.181.0
99
github.com/mithrandie/csvq-driver v1.6.8
10-
github.com/stretchr/testify v1.8.2
10+
github.com/stretchr/testify v1.8.3
1111
)
1212

1313
require (
14-
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
15-
github.com/go-logr/logr v1.2.3 // indirect
14+
cloud.google.com/go/compute/metadata v0.2.3 // indirect
15+
github.com/BurntSushi/toml v1.2.1 // indirect
16+
github.com/apache/arrow/go/v13 v13.0.0 // indirect
17+
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
18+
github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2 // indirect
19+
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
20+
github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 // indirect
21+
github.com/getkin/kin-openapi v0.120.0 // indirect
22+
github.com/go-logr/logr v1.2.4 // indirect
1623
github.com/go-logr/stdr v1.2.2 // indirect
17-
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
18-
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.40.0 // indirect
19-
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.37.0 // indirect
20-
go.opentelemetry.io/contrib/propagators/jaeger v1.15.0 // indirect
21-
go.opentelemetry.io/otel v1.14.0 // indirect
22-
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
23-
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
24-
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 // indirect
25-
go.opentelemetry.io/otel/metric v0.37.0 // indirect
26-
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
27-
go.opentelemetry.io/otel/trace v1.14.0 // indirect
28-
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
24+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
25+
github.com/go-openapi/swag v0.22.4 // indirect
26+
github.com/goccy/go-json v0.10.0 // indirect
27+
github.com/google/uuid v1.3.0 // indirect
28+
github.com/gorilla/mux v1.8.0 // indirect
29+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2 // indirect
30+
github.com/invopop/yaml v0.2.0 // indirect
31+
github.com/josharian/intern v1.0.0 // indirect
32+
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
33+
github.com/magefile/mage v1.15.0 // indirect
34+
github.com/mailru/easyjson v0.7.7 // indirect
35+
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
36+
github.com/perimeterx/marshmallow v1.1.5 // indirect
37+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
38+
github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8 // indirect
39+
github.com/unknwon/com v1.0.1 // indirect
40+
github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3 // indirect
41+
github.com/urfave/cli v1.22.12 // indirect
42+
github.com/zeebo/xxh3 v1.0.2 // indirect
43+
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.42.0 // indirect
44+
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.42.0 // indirect
45+
go.opentelemetry.io/contrib/propagators/jaeger v1.17.0 // indirect
46+
go.opentelemetry.io/otel v1.16.0 // indirect
47+
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
48+
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
49+
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0 // indirect
50+
go.opentelemetry.io/otel/metric v1.16.0 // indirect
51+
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
52+
go.opentelemetry.io/otel/trace v1.16.0 // indirect
53+
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
54+
golang.org/x/mod v0.9.0 // indirect
55+
golang.org/x/tools v0.6.0 // indirect
56+
google.golang.org/genproto/googleapis/api v0.0.0-20230731193218-e0aa005b6bdf // indirect
57+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf // indirect
58+
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
2959
)
3060

3161
require (
32-
github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 // indirect
3362
github.com/beorn7/perks v1.0.1 // indirect
3463
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3564
github.com/cheekybits/genny v1.0.0 // indirect
3665
github.com/davecgh/go-spew v1.1.1 // indirect
37-
github.com/fatih/color v1.7.0 // indirect
38-
github.com/golang/protobuf v1.5.2 // indirect
39-
github.com/golang/snappy v0.0.3 // indirect
40-
github.com/google/flatbuffers v2.0.0+incompatible // indirect
66+
github.com/fatih/color v1.15.0 // indirect
67+
github.com/golang/protobuf v1.5.3 // indirect
68+
github.com/google/flatbuffers v23.1.21+incompatible // indirect
4169
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
4270
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
43-
github.com/hashicorp/go-hclog v0.14.1 // indirect
44-
github.com/hashicorp/go-plugin v1.4.3 // indirect
45-
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
71+
github.com/hashicorp/go-hclog v1.5.0 // indirect
72+
github.com/hashicorp/go-plugin v1.4.9 // indirect
73+
github.com/hashicorp/yamux v0.1.1 // indirect
4674
github.com/json-iterator/go v1.1.12 // indirect
47-
github.com/klauspost/compress v1.13.1 // indirect
75+
github.com/klauspost/compress v1.15.15 // indirect
4876
github.com/mattetti/filebuffer v1.0.1 // indirect
49-
github.com/mattn/go-colorable v0.1.4 // indirect
50-
github.com/mattn/go-isatty v0.0.10 // indirect
77+
github.com/mattn/go-colorable v0.1.13 // indirect
78+
github.com/mattn/go-isatty v0.0.18 // indirect
5179
github.com/mattn/go-runewidth v0.0.9 // indirect
5280
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
5381
github.com/mitchellh/go-homedir v1.0.0 // indirect
54-
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
82+
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
5583
github.com/mithrandie/csvq v1.17.10 // indirect
5684
github.com/mithrandie/go-file/v2 v2.1.0 // indirect
5785
github.com/mithrandie/go-text v1.5.4 // indirect
5886
github.com/mithrandie/ternary v1.1.1 // indirect
5987
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
6088
github.com/modern-go/reflect2 v1.0.2 // indirect
61-
github.com/oklog/run v1.0.0 // indirect
89+
github.com/oklog/run v1.1.0 // indirect
6290
github.com/olekukonko/tablewriter v0.0.5 // indirect
63-
github.com/pierrec/lz4/v4 v4.1.8 // indirect
91+
github.com/pierrec/lz4/v4 v4.1.17 // indirect
6492
github.com/pmezard/go-difflib v1.0.0 // indirect
6593
github.com/prometheus/client_golang v1.14.0 // indirect
6694
github.com/prometheus/client_model v0.3.0 // indirect
67-
github.com/prometheus/common v0.40.0 // indirect
95+
github.com/prometheus/common v0.42.0 // indirect
6896
github.com/prometheus/procfs v0.8.0 // indirect
69-
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
97+
golang.org/x/crypto v0.11.0 // indirect
7098
golang.org/x/exp v0.0.0-20230307190834-24139beb5833
71-
golang.org/x/net v0.8.0 // indirect
72-
golang.org/x/sys v0.6.0 // indirect
73-
golang.org/x/term v0.6.0 // indirect
74-
golang.org/x/text v0.8.0 // indirect
75-
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
99+
golang.org/x/net v0.12.0 // indirect
100+
golang.org/x/sys v0.12.0 // indirect
101+
golang.org/x/term v0.10.0 // indirect
102+
golang.org/x/text v0.11.0 // indirect
103+
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
76104
google.golang.org/appengine v1.6.7 // indirect
77-
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
78-
google.golang.org/grpc v1.54.0 // indirect
79-
google.golang.org/protobuf v1.28.1 // indirect
105+
google.golang.org/grpc v1.57.0 // indirect
106+
google.golang.org/protobuf v1.31.0 // indirect
80107
gopkg.in/yaml.v3 v3.0.1 // indirect
81108
)

0 commit comments

Comments
 (0)