Plugins: Use error plane for api/ds/query (#54750)

* plugin client returns error base

* fix api test

* add plugin client test

* add fallback err

* fix linting

* wip

* replace bad query

* template is an error

* failing test of templated error

* add one test passing

* fix failing test

* move test

* rename ErrBadQuery to ErrQueryValidationFailure

* tidy diff

* Change to one error per specific error kind

* last err + fix test

* fix imports

* more tests

* keep req vars together

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
This commit is contained in:
Will Browne
2022-09-14 17:19:57 +01:00
committed by GitHub
parent deb86e3250
commit 29327cbba2
12 changed files with 342 additions and 66 deletions

View File

@@ -166,12 +166,16 @@ func (e Error) Is(other error) bool {
o, isGrafanaError := other.(Error)
//nolint:errorlint
base, isBase := other.(Base)
//nolint:errorlint
templateErr, isTemplateErr := other.(Template)
switch {
case isGrafanaError:
return o.Reason == e.Reason && o.MessageID == e.MessageID && o.Error() == e.Error()
case isBase:
return base.Is(e)
case isTemplateErr:
return templateErr.Base.Is(e)
default:
return false
}

View File

@@ -115,3 +115,7 @@ func (t Template) Build(data TemplateData) error {
return e
}
func (t Template) Error() string {
return t.Base.Error()
}

View File

@@ -3,10 +3,30 @@ package errutil_test
import (
"errors"
"fmt"
"testing"
"github.com/grafana/grafana/pkg/util/errutil"
"github.com/stretchr/testify/require"
)
func TestTemplate(t *testing.T) {
tmpl := errutil.NewBase(errutil.StatusInternal, "template.sample-error").MustTemplate("[{{ .Public.user }}] got error: {{ .Error }}")
err := tmpl.Build(errutil.TemplateData{
Public: map[string]interface{}{
"user": "grot the bot",
},
Error: errors.New("oh noes"),
})
t.Run("Built error should return true when compared with templated error ", func(t *testing.T) {
require.True(t, errors.Is(err, tmpl))
})
t.Run("Built error should return true when compared with templated error base ", func(t *testing.T) {
require.True(t, errors.Is(err, tmpl.Base))
})
}
func ExampleTemplate() {
// Initialization, this is typically done on a package or global
// level.