Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error source #99

Merged
merged 4 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
const defaultKeySuffix = "default"

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

mux := http.NewServeMux()
err = ds.registerRoutes(mux)
if err != nil {
return nil, err
return nil, PluginError(err)
}

ds.CallResourceHandler = httpadapter.New(mux)
Expand Down Expand Up @@ -130,6 +130,9 @@ func (ds *SQLDatasource) QueryData(ctx context.Context, req *backend.QueryDataRe
if err == nil {
if responseMutator, ok := ds.c.(ResponseMutator); ok {
frames, err = responseMutator.MutateResponse(ctx, frames)
if err != nil {
err = PluginError(err)
}
}
}
response.Set(query.RefID, backend.DataResponse{
Expand Down Expand Up @@ -178,7 +181,7 @@ func (ds *SQLDatasource) getDBConnectionFromQuery(q *Query, datasourceUID string

db, err := ds.c.Connect(dbConn.settings, q.ConnectionArgs)
if err != nil {
return "", dbConnection{}, err
return "", dbConnection{}, DownstreamError(err)
}
// Assign this connection in the cache
dbConn = dbConnection{db, dbConn.settings}
Expand Down Expand Up @@ -238,6 +241,8 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
return res, nil
}

// err = Unwrap(err)

if errors.Is(err, ErrorNoResults) {
return res, nil
}
Expand All @@ -251,7 +256,7 @@ func (ds *SQLDatasource) handleQuery(ctx context.Context, req backend.DataQuery,
backend.Logger.Warn(fmt.Sprintf("query failed: %s. Retrying %d times", err.Error(), i))
db, err := ds.dbReconnect(dbConn, q, cacheKey)
if err != nil {
return nil, err
return nil, DownstreamError(err)
}

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

db, err := ds.c.Connect(dbConn.settings, q.ConnectionArgs)
if err != nil {
return nil, err
return nil, DownstreamError(err)
}
ds.storeDBConnection(cacheKey, dbConnection{db, dbConn.settings})
return db, nil
Expand All @@ -306,7 +311,7 @@ func (ds *SQLDatasource) CheckHealth(ctx context.Context, req *backend.CheckHeal
key := defaultKey(getDatasourceUID(*req.PluginContext.DataSourceInstanceSettings))
dbConn, ok := ds.getDBConnection(key)
if !ok {
return nil, MissingDBConnection
return nil, ErrorMissingDBConnection
}

if ds.driverSettings.Retries == 0 {
Expand Down Expand Up @@ -361,7 +366,7 @@ func (ds *SQLDatasource) check(conn dbConnection) (*backend.CheckHealthResult, e
return &backend.CheckHealthResult{
Status: backend.HealthStatusError,
Message: err.Error(),
}, err
}, DownstreamError(err)
}

return &backend.CheckHealthResult{
Expand Down
14 changes: 13 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package sqlds

import "errors"
import (
"errors"

es "github.com/grafana/grafana-plugin-sdk-go/experimental/errorsource"
)

var (
// ErrorBadDatasource is returned if the data source could not be asserted to the correct type (this should basically never happen?)
Expand All @@ -14,3 +18,11 @@ var (
// ErrorNoResults is returned if there were no results returned
ErrorNoResults = errors.New("no results returned from query")
)

func PluginError(err error, override ...bool) error {
return es.PluginError(err, len(override) > 0)
}

func DownstreamError(err error, override ...bool) error {
return es.DownstreamError(err, len(override) > 0)
}
107 changes: 67 additions & 40 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,104 @@ go 1.20
require (
github.com/go-sql-driver/mysql v1.4.0
github.com/google/go-cmp v0.5.9
github.com/grafana/grafana-plugin-sdk-go v0.159.0
github.com/grafana/grafana-plugin-sdk-go v0.181.0
github.com/mithrandie/csvq-driver v1.6.8
github.com/stretchr/testify v1.8.2
github.com/stretchr/testify v1.8.3
)

require (
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/BurntSushi/toml v1.2.1 // indirect
github.com/apache/arrow/go/v13 v13.0.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/chromedp/cdproto v0.0.0-20220208224320-6efb837e6bc2 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/elazarl/goproxy v0.0.0-20230731152917-f99041a5c027 // indirect
github.com/getkin/kin-openapi v0.120.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.40.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.37.0 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.15.0 // indirect
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.14.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.14.0 // indirect
go.opentelemetry.io/otel/metric v0.37.0 // indirect
go.opentelemetry.io/otel/sdk v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.2 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.3 // indirect
github.com/magefile/mage v1.15.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/unknwon/bra v0.0.0-20200517080246-1e3013ecaff8 // indirect
github.com/unknwon/com v1.0.1 // indirect
github.com/unknwon/log v0.0.0-20150304194804-e617c87089d3 // indirect
github.com/urfave/cli v1.22.12 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.42.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/httptrace/otelhttptrace v0.42.0 // indirect
go.opentelemetry.io/contrib/propagators/jaeger v1.17.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.16.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230731193218-e0aa005b6bdf // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230731193218-e0aa005b6bdf // indirect
gopkg.in/fsnotify/fsnotify.v1 v1.4.7 // indirect
)

require (
github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cheekybits/genny v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v2.0.0+incompatible // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/flatbuffers v23.1.21+incompatible // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
github.com/hashicorp/go-hclog v0.14.1 // indirect
github.com/hashicorp/go-plugin v1.4.3 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
github.com/hashicorp/go-plugin v1.4.9 // indirect
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.1 // indirect
github.com/klauspost/compress v1.15.15 // indirect
github.com/mattetti/filebuffer v1.0.1 // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/go-homedir v1.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mithrandie/csvq v1.17.10 // indirect
github.com/mithrandie/go-file/v2 v2.1.0 // indirect
github.com/mithrandie/go-text v1.5.4 // indirect
github.com/mithrandie/ternary v1.1.1 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pierrec/lz4/v4 v4.1.8 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.40.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230307190834-24139beb5833
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.54.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading