mirror of
https://github.com/grafana/grafana.git
synced 2025-08-01 03:01:51 +08:00

* 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>
75 lines
2.0 KiB
Go
75 lines
2.0 KiB
Go
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.
|
|
var tmpl = errutil.NewBase(errutil.StatusInternal, "template.sample-error").MustTemplate("[{{ .Public.user }}] got error: {{ .Error }}")
|
|
|
|
// Construct an error based on the template.
|
|
err := tmpl.Build(errutil.TemplateData{
|
|
Public: map[string]interface{}{
|
|
"user": "grot the bot",
|
|
},
|
|
Error: errors.New("oh noes"),
|
|
})
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
// Output:
|
|
// [template.sample-error] [grot the bot] got error: oh noes
|
|
}
|
|
|
|
func ExampleTemplate_public() {
|
|
// Initialization, this is typically done on a package or global
|
|
// level.
|
|
var tmpl = errutil.
|
|
NewBase(errutil.StatusInternal, "template.sample-error").
|
|
MustTemplate(
|
|
"[{{ .Public.user }}] got error: {{ .Error }}",
|
|
errutil.WithPublic("Oh, no, error for {{ .Public.user }}"),
|
|
)
|
|
|
|
// Construct an error based on the template.
|
|
//nolint:errorlint
|
|
err := tmpl.Build(errutil.TemplateData{
|
|
Public: map[string]interface{}{
|
|
"user": "grot the bot",
|
|
},
|
|
Error: errors.New("oh noes"),
|
|
}).(errutil.Error)
|
|
|
|
fmt.Println(err.Error())
|
|
fmt.Println(err.PublicMessage)
|
|
|
|
// Output:
|
|
// [template.sample-error] [grot the bot] got error: oh noes
|
|
// Oh, no, error for grot the bot
|
|
}
|