Files
grafana/pkg/plugins/errors_test.go
Ivana Huckova 54a51bd3e3 Data source plugin: Improve error message when plugin has connection issues (#102625)
* Improve data source error message when stackID

* Update comment

* Revert "Update comment"

This reverts commit 48922bc55259803f1717e91afc9f749a60d61184.

* Revert "Improve data source error message when stackID"

This reverts commit 4bf0a2f7b712e77b9de4655716695a7ee75c183b.

* Make public messagic configurable based on context

* Update, simplify

* Update

* Update getting stack

* Update pkg/plugins/errors.go

Co-authored-by: Andres Martinez Gotor <andres.martinez@grafana.com>

* Refactor test to test for when context has stack value

* Remove duplicated test

* Fix error checking logic

---------

Co-authored-by: Andres Martinez Gotor <andres.martinez@grafana.com>
2025-03-31 12:00:40 +02:00

59 lines
1.7 KiB
Go

package plugins
import (
"context"
"errors"
"testing"
"github.com/grafana/grafana/pkg/apimachinery/errutil"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/stretchr/testify/assert"
)
func TestErrPluginGrpcConnectionUnavailableBase(t *testing.T) {
tests := []struct {
name string
ctx context.Context
err error
expectedPublic string
}{
{
name: "without stack ID in context",
ctx: identity.WithRequester(context.Background(), &identity.StaticRequester{
Namespace: "org-123",
}),
err: errors.New("connection failed"),
expectedPublic: "Data source became unavailable during request. Please try again.",
},
{
name: "with stack ID in context",
ctx: identity.WithRequester(context.Background(), &identity.StaticRequester{
Namespace: "stacks-123",
}),
err: errors.New("connection failed"),
expectedPublic: "Data source became unavailable during request. Please try again. If the problem persists, please contact customer support.",
},
{
name: "without static requester in context",
ctx: context.Background(),
err: errors.New("connection failed"),
expectedPublic: "Data source became unavailable during request. Please try again.",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := ErrPluginGrpcConnectionUnavailableBaseFn(tt.ctx).Errorf("%v", tt.err)
assert.Error(t, err)
// Check if it's a Grafana error
var grafanaErr errutil.Error
assert.ErrorAs(t, err, &grafanaErr)
// Check the public message
publicErr := grafanaErr.Public()
assert.Equal(t, tt.expectedPublic, publicErr.Message)
})
}
}