Chore: Refactor backend plugin errors (#74928)
This commit is contained in:
@@ -10,7 +10,6 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||
"github.com/grafana/grafana/pkg/plugins/backendplugin/instrumentation"
|
||||
"github.com/grafana/grafana/pkg/plugins/config"
|
||||
"github.com/grafana/grafana/pkg/plugins/manager/registry"
|
||||
@@ -48,7 +47,7 @@ func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest)
|
||||
|
||||
p, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
if !exists {
|
||||
return nil, plugins.ErrPluginNotRegistered.Errorf("%w", backendplugin.ErrPluginNotRegistered)
|
||||
return nil, plugins.ErrPluginNotRegistered
|
||||
}
|
||||
|
||||
var totalBytes float64
|
||||
@@ -65,15 +64,15 @@ func (s *Service) QueryData(ctx context.Context, req *backend.QueryDataRequest)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, backendplugin.ErrMethodNotImplemented) {
|
||||
return nil, plugins.ErrMethodNotImplemented.Errorf("%w", backendplugin.ErrMethodNotImplemented)
|
||||
if errors.Is(err, plugins.ErrMethodNotImplemented) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if errors.Is(err, backendplugin.ErrPluginUnavailable) {
|
||||
return nil, plugins.ErrPluginUnavailable.Errorf("%w", backendplugin.ErrPluginUnavailable)
|
||||
if errors.Is(err, plugins.ErrPluginUnavailable) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, plugins.ErrPluginDownstreamError.Errorf("client: failed to query data: %w", err)
|
||||
return nil, plugins.ErrPluginDownstreamErrorBase.Errorf("client: failed to query data: %w", err)
|
||||
}
|
||||
|
||||
for refID, res := range resp.Responses {
|
||||
@@ -99,7 +98,7 @@ func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceReq
|
||||
|
||||
p, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
if !exists {
|
||||
return backendplugin.ErrPluginNotRegistered
|
||||
return plugins.ErrPluginNotRegistered
|
||||
}
|
||||
|
||||
totalBytes := float64(len(req.Body))
|
||||
@@ -132,7 +131,7 @@ func (s *Service) CallResource(ctx context.Context, req *backend.CallResourceReq
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return plugins.ErrPluginDownstreamError.Errorf("client: failed to call resources: %w", err)
|
||||
return plugins.ErrPluginDownstreamErrorBase.Errorf("client: failed to call resources: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -145,7 +144,7 @@ func (s *Service) CollectMetrics(ctx context.Context, req *backend.CollectMetric
|
||||
|
||||
p, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
if !exists {
|
||||
return nil, backendplugin.ErrPluginNotRegistered
|
||||
return nil, plugins.ErrPluginNotRegistered
|
||||
}
|
||||
|
||||
var resp *backend.CollectMetricsResult
|
||||
@@ -156,7 +155,7 @@ func (s *Service) CollectMetrics(ctx context.Context, req *backend.CollectMetric
|
||||
return
|
||||
})
|
||||
if err != nil {
|
||||
return nil, plugins.ErrPluginDownstreamError.Errorf("client: failed to collect metrics: %w", err)
|
||||
return nil, plugins.ErrPluginDownstreamErrorBase.Errorf("client: failed to collect metrics: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
@@ -169,7 +168,7 @@ func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthReque
|
||||
|
||||
p, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
if !exists {
|
||||
return nil, backendplugin.ErrPluginNotRegistered
|
||||
return nil, plugins.ErrPluginNotRegistered
|
||||
}
|
||||
|
||||
var resp *backend.CheckHealthResult
|
||||
@@ -181,15 +180,15 @@ func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthReque
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if errors.Is(err, backendplugin.ErrMethodNotImplemented) {
|
||||
if errors.Is(err, plugins.ErrMethodNotImplemented) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if errors.Is(err, backendplugin.ErrPluginUnavailable) {
|
||||
if errors.Is(err, plugins.ErrPluginUnavailable) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nil, plugins.ErrPluginDownstreamError.Errorf("client: failed to check health: %w", err)
|
||||
return nil, plugins.ErrPluginDownstreamErrorBase.Errorf("client: failed to check health: %w", err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
@@ -202,7 +201,7 @@ func (s *Service) SubscribeStream(ctx context.Context, req *backend.SubscribeStr
|
||||
|
||||
plugin, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
if !exists {
|
||||
return nil, backendplugin.ErrPluginNotRegistered
|
||||
return nil, plugins.ErrPluginNotRegistered
|
||||
}
|
||||
|
||||
return plugin.SubscribeStream(ctx, req)
|
||||
@@ -215,7 +214,7 @@ func (s *Service) PublishStream(ctx context.Context, req *backend.PublishStreamR
|
||||
|
||||
plugin, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
if !exists {
|
||||
return nil, backendplugin.ErrPluginNotRegistered
|
||||
return nil, plugins.ErrPluginNotRegistered
|
||||
}
|
||||
|
||||
return plugin.PublishStream(ctx, req)
|
||||
@@ -232,7 +231,7 @@ func (s *Service) RunStream(ctx context.Context, req *backend.RunStreamRequest,
|
||||
|
||||
plugin, exists := s.plugin(ctx, req.PluginContext.PluginID)
|
||||
if !exists {
|
||||
return backendplugin.ErrPluginNotRegistered
|
||||
return plugins.ErrPluginNotRegistered
|
||||
}
|
||||
|
||||
return plugin.RunStream(ctx, req, sender)
|
||||
|
||||
@@ -30,16 +30,16 @@ func TestQueryData(t *testing.T) {
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
err: backendplugin.ErrPluginUnavailable,
|
||||
err: plugins.ErrPluginUnavailable,
|
||||
expectedError: plugins.ErrPluginUnavailable,
|
||||
},
|
||||
{
|
||||
err: backendplugin.ErrMethodNotImplemented,
|
||||
err: plugins.ErrMethodNotImplemented,
|
||||
expectedError: plugins.ErrMethodNotImplemented,
|
||||
},
|
||||
{
|
||||
err: errors.New("surprise surprise"),
|
||||
expectedError: plugins.ErrPluginDownstreamError,
|
||||
expectedError: plugins.ErrPluginDownstreamErrorBase,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func TestCheckHealth(t *testing.T) {
|
||||
client := ProvideService(registry, &config.Cfg{})
|
||||
_, err := client.CheckHealth(context.Background(), &backend.CheckHealthRequest{})
|
||||
require.Error(t, err)
|
||||
require.ErrorIs(t, err, backendplugin.ErrPluginNotRegistered)
|
||||
require.ErrorIs(t, err, plugins.ErrPluginNotRegistered)
|
||||
})
|
||||
|
||||
t.Run("non-empty plugin registry", func(t *testing.T) {
|
||||
@@ -87,17 +87,17 @@ func TestCheckHealth(t *testing.T) {
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
err: backendplugin.ErrPluginUnavailable,
|
||||
expectedError: backendplugin.ErrPluginUnavailable,
|
||||
err: plugins.ErrPluginUnavailable,
|
||||
expectedError: plugins.ErrPluginUnavailable,
|
||||
},
|
||||
{
|
||||
|
||||
err: backendplugin.ErrMethodNotImplemented,
|
||||
expectedError: backendplugin.ErrMethodNotImplemented,
|
||||
err: plugins.ErrMethodNotImplemented,
|
||||
expectedError: plugins.ErrMethodNotImplemented,
|
||||
},
|
||||
{
|
||||
err: errors.New("surprise surprise"),
|
||||
expectedError: plugins.ErrPluginDownstreamError,
|
||||
expectedError: plugins.ErrPluginDownstreamErrorBase,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -127,7 +127,7 @@ func (pc *FakePluginClient) CollectMetrics(ctx context.Context, req *backend.Col
|
||||
return pc.CollectMetricsHandlerFunc(ctx, req)
|
||||
}
|
||||
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (pc *FakePluginClient) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
|
||||
@@ -135,7 +135,7 @@ func (pc *FakePluginClient) CheckHealth(ctx context.Context, req *backend.CheckH
|
||||
return pc.CheckHealthHandlerFunc(ctx, req)
|
||||
}
|
||||
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (pc *FakePluginClient) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
||||
@@ -143,7 +143,7 @@ func (pc *FakePluginClient) QueryData(ctx context.Context, req *backend.QueryDat
|
||||
return pc.QueryDataHandlerFunc(ctx, req)
|
||||
}
|
||||
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (pc *FakePluginClient) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
|
||||
@@ -151,19 +151,19 @@ func (pc *FakePluginClient) CallResource(ctx context.Context, req *backend.CallR
|
||||
return pc.CallResourceHandlerFunc(ctx, req, sender)
|
||||
}
|
||||
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (pc *FakePluginClient) SubscribeStream(_ context.Context, _ *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (pc *FakePluginClient) PublishStream(_ context.Context, _ *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
|
||||
return nil, backendplugin.ErrMethodNotImplemented
|
||||
return nil, plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
func (pc *FakePluginClient) RunStream(_ context.Context, _ *backend.RunStreamRequest, _ *backend.StreamSender) error {
|
||||
return backendplugin.ErrMethodNotImplemented
|
||||
return plugins.ErrMethodNotImplemented
|
||||
}
|
||||
|
||||
type FakePluginRegistry struct {
|
||||
|
||||
Reference in New Issue
Block a user