sql: remove the usage of "errutil" (#81888)

This commit is contained in:
Gábor Farkas 2024-02-07 08:39:24 +01:00 committed by GitHub
parent 40a08a7720
commit 7b8c7b623c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 39 deletions

View File

@ -282,7 +282,7 @@ func (t *mssqlQueryResultTransformer) TransformQueryError(logger log.Logger, err
// ref https://github.com/denisenkom/go-mssqldb/blob/045585d74f9069afe2e115b6235eb043c8047043/tds.go#L904 // ref https://github.com/denisenkom/go-mssqldb/blob/045585d74f9069afe2e115b6235eb043c8047043/tds.go#L904
if strings.HasPrefix(strings.ToLower(err.Error()), "unable to open tcp connection with host") { if strings.HasPrefix(strings.ToLower(err.Error()), "unable to open tcp connection with host") {
logger.Error("Query error", "error", err) logger.Error("Query error", "error", err)
return sqleng.ErrConnectionFailed.Errorf("failed to connect to server - %s", t.userError) return fmt.Errorf("failed to connect to server - %s", t.userError)
} }
return err return err

View File

@ -1313,23 +1313,23 @@ func TestMSSQL(t *testing.T) {
func TestTransformQueryError(t *testing.T) { func TestTransformQueryError(t *testing.T) {
transformer := &mssqlQueryResultTransformer{} transformer := &mssqlQueryResultTransformer{}
randomErr := fmt.Errorf("random error")
tests := []struct {
err error
expectedErr error
}{
{err: fmt.Errorf("Unable to open tcp connection with host 'localhost:5000': dial tcp: connection refused"), expectedErr: sqleng.ErrConnectionFailed},
{err: fmt.Errorf("unable to open tcp connection with host 'localhost:5000': dial tcp: connection refused"), expectedErr: sqleng.ErrConnectionFailed},
{err: randomErr, expectedErr: randomErr},
}
logger := backend.NewLoggerWith("logger", "mssql.test") logger := backend.NewLoggerWith("logger", "mssql.test")
for _, tc := range tests { t.Run("Should not return a connection error", func(t *testing.T) {
resultErr := transformer.TransformQueryError(logger, tc.err) err := fmt.Errorf("Unable to open tcp connection with host 'localhost:5000': dial tcp: connection refused")
assert.ErrorIs(t, resultErr, tc.expectedErr) resultErr := transformer.TransformQueryError(logger, err)
} errorText := resultErr.Error()
assert.NotEqual(t, err, resultErr)
assert.NotContains(t, errorText, "Unable to open tcp connection with host")
assert.Contains(t, errorText, "failed to connect to server")
})
t.Run("Should return a non-connection error unmodified", func(t *testing.T) {
err := fmt.Errorf("normal error")
resultErr := transformer.TransformQueryError(logger, err)
assert.Equal(t, err, resultErr)
assert.ErrorIs(t, err, resultErr)
})
} }
func TestGenerateConnectionString(t *testing.T) { func TestGenerateConnectionString(t *testing.T) {

View File

@ -21,14 +21,11 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend/gtime" "github.com/grafana/grafana-plugin-sdk-go/backend/gtime"
"github.com/grafana/grafana-plugin-sdk-go/backend/log" "github.com/grafana/grafana-plugin-sdk-go/backend/log"
"github.com/grafana/grafana/pkg/setting" "github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/util/errutil"
) )
// MetaKeyExecutedQueryString is the key where the executed query should get stored // MetaKeyExecutedQueryString is the key where the executed query should get stored
const MetaKeyExecutedQueryString = "executedQueryString" const MetaKeyExecutedQueryString = "executedQueryString"
var ErrConnectionFailed = errutil.Internal("sqleng.connectionError")
// SQLMacroEngine interpolates macros into sql. It takes in the Query to have access to query context and // SQLMacroEngine interpolates macros into sql. It takes in the Query to have access to query context and
// timeRange to be able to generate queries that use from and to. // timeRange to be able to generate queries that use from and to.
type SQLMacroEngine interface { type SQLMacroEngine interface {
@ -112,7 +109,7 @@ func (e *DataSourceHandler) TransformQueryError(logger log.Logger, err error) er
var opErr *net.OpError var opErr *net.OpError
if errors.As(err, &opErr) { if errors.As(err, &opErr) {
logger.Error("Query error", "err", err) logger.Error("Query error", "err", err)
return ErrConnectionFailed.Errorf("failed to connect to server - %s", e.userError) return fmt.Errorf("failed to connect to server - %s", e.userError)
} }
return e.queryResultTransformer.TransformQueryError(logger, err) return e.queryResultTransformer.TransformQueryError(logger, err)

View File

@ -400,28 +400,32 @@ func TestSQLEngine(t *testing.T) {
} }
}) })
t.Run("Should handle connection errors", func(t *testing.T) { t.Run("Should not return raw connection errors", func(t *testing.T) {
randomErr := fmt.Errorf("random error") err := net.OpError{Op: "Dial", Err: fmt.Errorf("inner-error")}
transformer := &testQueryResultTransformer{}
tests := []struct { dp := DataSourceHandler{
err error log: backend.NewLoggerWith("logger", "test"),
expectedErr error queryResultTransformer: transformer,
expectQueryResultTransformerWasCalled bool
}{
{err: &net.OpError{Op: "Dial", Err: fmt.Errorf("inner-error")}, expectedErr: ErrConnectionFailed, expectQueryResultTransformerWasCalled: false},
{err: randomErr, expectedErr: randomErr, expectQueryResultTransformerWasCalled: true},
} }
resultErr := dp.TransformQueryError(dp.log, &err)
assert.False(t, transformer.transformQueryErrorWasCalled)
errorText := resultErr.Error()
assert.NotEqual(t, err, resultErr)
assert.NotContains(t, errorText, "inner-error")
assert.Contains(t, errorText, "failed to connect to server")
})
for _, tc := range tests { t.Run("Should return non-connection errors unmodified", func(t *testing.T) {
transformer := &testQueryResultTransformer{} err := fmt.Errorf("normal error")
dp := DataSourceHandler{ transformer := &testQueryResultTransformer{}
log: backend.NewLoggerWith("logger", "test"), dp := DataSourceHandler{
queryResultTransformer: transformer, log: backend.NewLoggerWith("logger", "test"),
} queryResultTransformer: transformer,
resultErr := dp.TransformQueryError(dp.log, tc.err)
assert.ErrorIs(t, resultErr, tc.expectedErr)
assert.Equal(t, tc.expectQueryResultTransformerWasCalled, transformer.transformQueryErrorWasCalled)
} }
resultErr := dp.TransformQueryError(dp.log, err)
assert.True(t, transformer.transformQueryErrorWasCalled)
assert.Equal(t, err, resultErr)
assert.ErrorIs(t, err, resultErr)
}) })
} }