2022-06-14 03:50:11 -05:00
|
|
|
package errutil_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-09-14 11:19:57 -05:00
|
|
|
"testing"
|
2022-06-14 03:50:11 -05:00
|
|
|
|
2022-09-14 11:19:57 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-10-07 05:47:43 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/util/errutil"
|
2022-06-14 03:50:11 -05:00
|
|
|
)
|
|
|
|
|
2022-09-14 11:19:57 -05:00
|
|
|
func TestTemplate(t *testing.T) {
|
2022-10-07 05:47:43 -05:00
|
|
|
tmpl := errutil.NewBase(errutil.StatusInternal, "template.sampleError").MustTemplate("[{{ .Public.user }}] got error: {{ .Error }}")
|
2022-09-14 11:19:57 -05:00
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-14 03:50:11 -05:00
|
|
|
func ExampleTemplate() {
|
|
|
|
// Initialization, this is typically done on a package or global
|
|
|
|
// level.
|
2022-10-07 05:47:43 -05:00
|
|
|
var tmpl = errutil.NewBase(errutil.StatusInternal, "template.sampleError").MustTemplate("[{{ .Public.user }}] got error: {{ .Error }}")
|
2022-06-14 03:50:11 -05:00
|
|
|
|
|
|
|
// 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:
|
2022-10-07 05:47:43 -05:00
|
|
|
// [template.sampleError] [grot the bot] got error: oh noes
|
2022-06-14 03:50:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func ExampleTemplate_public() {
|
|
|
|
// Initialization, this is typically done on a package or global
|
|
|
|
// level.
|
|
|
|
var tmpl = errutil.
|
2022-10-07 05:47:43 -05:00
|
|
|
NewBase(errutil.StatusInternal, "template.sampleError").
|
2022-06-14 03:50:11 -05:00
|
|
|
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:
|
2022-10-07 05:47:43 -05:00
|
|
|
// [template.sampleError] [grot the bot] got error: oh noes
|
2022-06-14 03:50:11 -05:00
|
|
|
// Oh, no, error for grot the bot
|
|
|
|
}
|