Chore: Refactor backend plugin errors (#74928)

This commit is contained in:
Andres Martinez Gotor
2023-09-25 11:56:03 +02:00
committed by GitHub
parent 38d2357bb8
commit 1714fa598c
16 changed files with 102 additions and 130 deletions

View File

@ -235,15 +235,15 @@ func TestDataSourceQueryError(t *testing.T) {
}{
{
request: reqValid,
clientErr: backendplugin.ErrPluginUnavailable,
clientErr: plugins.ErrPluginUnavailable,
expectedStatus: http.StatusInternalServerError,
expectedBody: `{"message":"Internal server error","messageId":"plugin.unavailable","statusCode":500,"traceID":""}`,
expectedBody: `{"message":"Plugin unavailable","messageId":"plugin.unavailable","statusCode":500,"traceID":""}`,
},
{
request: reqValid,
clientErr: backendplugin.ErrMethodNotImplemented,
expectedStatus: http.StatusNotImplemented,
expectedBody: `{"message":"Not implemented","messageId":"plugin.notImplemented","statusCode":501,"traceID":""}`,
clientErr: plugins.ErrMethodNotImplemented,
expectedStatus: http.StatusNotFound,
expectedBody: `{"message":"Method not implemented","messageId":"plugin.notImplemented","statusCode":404,"traceID":""}`,
},
{
request: reqValid,

View File

@ -7,7 +7,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/web"
)
@ -30,7 +30,7 @@ func (hs *HTTPServer) pluginMetricsEndpoint(ctx *web.Context) {
resp, err := hs.pluginClient.CollectMetrics(ctx.Req.Context(), &backend.CollectMetricsRequest{PluginContext: backend.PluginContext{PluginID: pluginID}})
if err != nil {
if errors.Is(err, backendplugin.ErrPluginNotRegistered) {
if errors.Is(err, plugins.ErrPluginNotRegistered) {
ctx.Resp.WriteHeader(http.StatusNotFound)
return
}

View File

@ -11,7 +11,6 @@ import (
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/web/webtest"
)
@ -152,7 +151,7 @@ func (c *fakePluginClientMetrics) CollectMetrics(ctx context.Context, req *backe
metrics, exists := c.store[req.PluginContext.PluginID]
if !exists {
return nil, backendplugin.ErrPluginNotRegistered
return nil, plugins.ErrPluginNotRegistered
}
return &backend.CollectMetricsResult{

View File

@ -11,7 +11,6 @@ import (
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/middleware/requestmeta"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/plugins/httpresponsesender"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/datasources"
@ -125,16 +124,6 @@ func (hs *HTTPServer) makePluginResourceRequest(w http.ResponseWriter, req *http
}
func handleCallResourceError(err error, reqCtx *contextmodel.ReqContext) {
if errors.Is(err, backendplugin.ErrPluginUnavailable) {
reqCtx.JsonApiErr(http.StatusServiceUnavailable, "Plugin unavailable", err)
return
}
if errors.Is(err, backendplugin.ErrMethodNotImplemented) {
reqCtx.JsonApiErr(http.StatusNotFound, "Not found", err)
return
}
resp := response.ErrOrFallback(http.StatusInternalServerError, "Failed to call resource", err)
resp.WriteTo(reqCtx)
}

View File

@ -20,7 +20,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/plugins/repo"
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
@ -492,18 +491,6 @@ func (hs *HTTPServer) UninstallPlugin(c *contextmodel.ReqContext) response.Respo
}
func translatePluginRequestErrorToAPIError(err error) response.Response {
if errors.Is(err, backendplugin.ErrPluginNotRegistered) {
return response.Error(http.StatusNotFound, "Plugin not found", err)
}
if errors.Is(err, backendplugin.ErrMethodNotImplemented) {
return response.Error(http.StatusNotFound, "Not found", err)
}
if errors.Is(err, backendplugin.ErrPluginUnavailable) {
return response.Error(http.StatusServiceUnavailable, "Plugin unavailable", err)
}
return response.ErrOrFallback(http.StatusInternalServerError, "Plugin request failed", err)
}

View File

@ -5,6 +5,7 @@ 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/log"
)
@ -70,7 +71,7 @@ func (cp *corePlugin) Target() backendplugin.Target {
}
func (cp *corePlugin) CollectMetrics(_ context.Context, _ *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (cp *corePlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
@ -78,7 +79,7 @@ func (cp *corePlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthR
return cp.CheckHealthHandler.CheckHealth(ctx, req)
}
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (cp *corePlugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
@ -86,7 +87,7 @@ func (cp *corePlugin) QueryData(ctx context.Context, req *backend.QueryDataReque
return cp.QueryDataHandler.QueryData(ctx, req)
}
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (cp *corePlugin) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
@ -94,26 +95,26 @@ func (cp *corePlugin) CallResource(ctx context.Context, req *backend.CallResourc
return cp.CallResourceHandler.CallResource(ctx, req, sender)
}
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
func (cp *corePlugin) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
if cp.StreamHandler != nil {
return cp.StreamHandler.SubscribeStream(ctx, req)
}
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (cp *corePlugin) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
if cp.StreamHandler != nil {
return cp.StreamHandler.PublishStream(ctx, req)
}
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (cp *corePlugin) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
if cp.StreamHandler != nil {
return cp.StreamHandler.RunStream(ctx, req, sender)
}
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}

View File

@ -5,7 +5,7 @@ import (
"testing"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin"
"github.com/grafana/grafana/pkg/plugins/log"
"github.com/stretchr/testify/require"
@ -23,13 +23,13 @@ func TestCorePlugin(t *testing.T) {
require.False(t, p.Exited())
_, err = p.CollectMetrics(context.Background(), &backend.CollectMetricsRequest{})
require.Equal(t, backendplugin.ErrMethodNotImplemented, err)
require.Equal(t, plugins.ErrMethodNotImplemented, err)
_, err = p.CheckHealth(context.Background(), nil)
require.Equal(t, backendplugin.ErrMethodNotImplemented, err)
require.Equal(t, plugins.ErrMethodNotImplemented, err)
err = p.CallResource(context.Background(), nil, nil)
require.Equal(t, backendplugin.ErrMethodNotImplemented, err)
require.Equal(t, plugins.ErrMethodNotImplemented, err)
})
t.Run("New core plugin with handlers set in opts should return expected values", func(t *testing.T) {
@ -56,7 +56,7 @@ func TestCorePlugin(t *testing.T) {
require.False(t, p.Exited())
_, err = p.CollectMetrics(context.Background(), &backend.CollectMetricsRequest{})
require.Equal(t, backendplugin.ErrMethodNotImplemented, err)
require.Equal(t, plugins.ErrMethodNotImplemented, err)
_, err = p.CheckHealth(context.Background(), &backend.CheckHealthRequest{})
require.NoError(t, err)

View File

@ -1,12 +0,0 @@
package backendplugin
import "errors"
var (
// ErrPluginNotRegistered error returned when plugin is not registered.
ErrPluginNotRegistered = errors.New("plugin not registered")
// ErrPluginUnavailable error returned when plugin is unavailable.
ErrPluginUnavailable = errors.New("plugin unavailable")
// ErrMethodNotImplemented error returned when plugin method not implemented.
ErrMethodNotImplemented = errors.New("method not implemented")
)

View File

@ -13,7 +13,7 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/backendplugin/pluginextensionv2"
"github.com/grafana/grafana/pkg/plugins/backendplugin/secretsmanagerplugin"
"github.com/grafana/grafana/pkg/plugins/log"
@ -130,7 +130,7 @@ func (c *ClientV2) CollectMetrics(ctx context.Context, req *backend.CollectMetri
func (c *ClientV2) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
if c.DiagnosticsClient == nil {
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
protoContext := backend.ToProto().PluginContext(req.PluginContext)
@ -151,7 +151,7 @@ func (c *ClientV2) CheckHealth(ctx context.Context, req *backend.CheckHealthRequ
func (c *ClientV2) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
if c.DataClient == nil {
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
protoReq := backend.ToProto().QueryDataRequest(req)
@ -159,7 +159,7 @@ func (c *ClientV2) QueryData(ctx context.Context, req *backend.QueryDataRequest)
if err != nil {
if status.Code(err) == codes.Unimplemented {
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
return nil, fmt.Errorf("%v: %w", "Failed to query data", err)
@ -170,14 +170,14 @@ func (c *ClientV2) QueryData(ctx context.Context, req *backend.QueryDataRequest)
func (c *ClientV2) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
if c.ResourceClient == nil {
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
protoReq := backend.ToProto().CallResourceRequest(req)
protoStream, err := c.ResourceClient.CallResource(ctx, protoReq)
if err != nil {
if status.Code(err) == codes.Unimplemented {
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
return fmt.Errorf("%v: %w", "Failed to call resource", err)
@ -187,7 +187,7 @@ func (c *ClientV2) CallResource(ctx context.Context, req *backend.CallResourceRe
protoResp, err := protoStream.Recv()
if err != nil {
if status.Code(err) == codes.Unimplemented {
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
if errors.Is(err, io.EOF) {
@ -205,7 +205,7 @@ func (c *ClientV2) CallResource(ctx context.Context, req *backend.CallResourceRe
func (c *ClientV2) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
if c.StreamClient == nil {
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
protoResp, err := c.StreamClient.SubscribeStream(ctx, backend.ToProto().SubscribeStreamRequest(req))
if err != nil {
@ -216,7 +216,7 @@ func (c *ClientV2) SubscribeStream(ctx context.Context, req *backend.SubscribeSt
func (c *ClientV2) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
if c.StreamClient == nil {
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
protoResp, err := c.StreamClient.PublishStream(ctx, backend.ToProto().PublishStreamRequest(req))
if err != nil {
@ -227,14 +227,14 @@ func (c *ClientV2) PublishStream(ctx context.Context, req *backend.PublishStream
func (c *ClientV2) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
if c.StreamClient == nil {
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
protoReq := backend.ToProto().RunStreamRequest(req)
protoStream, err := c.StreamClient.RunStream(ctx, protoReq)
if err != nil {
if status.Code(err) == codes.Unimplemented {
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
return fmt.Errorf("%v: %w", "Failed to call resource", err)
}
@ -243,7 +243,7 @@ func (c *ClientV2) RunStream(ctx context.Context, req *backend.RunStreamRequest,
p, err := protoStream.Recv()
if err != nil {
if status.Code(err) == codes.Unimplemented {
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
if errors.Is(err, io.EOF) {
return nil

View File

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/go-plugin"
"github.com/grafana/grafana/pkg/infra/process"
"github.com/grafana/grafana/pkg/plugins"
"github.com/grafana/grafana/pkg/plugins/backendplugin"
"github.com/grafana/grafana/pkg/plugins/log"
)
@ -141,7 +142,7 @@ func (p *grpcPlugin) getPluginClient() (pluginClient, bool) {
func (p *grpcPlugin) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
pluginClient, ok := p.getPluginClient()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, plugins.ErrPluginUnavailable
}
return pluginClient.CollectMetrics(ctx, req)
}
@ -149,7 +150,7 @@ func (p *grpcPlugin) CollectMetrics(ctx context.Context, req *backend.CollectMet
func (p *grpcPlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
pluginClient, ok := p.getPluginClient()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, plugins.ErrPluginUnavailable
}
return pluginClient.CheckHealth(ctx, req)
}
@ -157,7 +158,7 @@ func (p *grpcPlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRe
func (p *grpcPlugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
pluginClient, ok := p.getPluginClient()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, plugins.ErrPluginUnavailable
}
return pluginClient.QueryData(ctx, req)
@ -166,7 +167,7 @@ func (p *grpcPlugin) QueryData(ctx context.Context, req *backend.QueryDataReques
func (p *grpcPlugin) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
pluginClient, ok := p.getPluginClient()
if !ok {
return backendplugin.ErrPluginUnavailable
return plugins.ErrPluginUnavailable
}
return pluginClient.CallResource(ctx, req, sender)
}
@ -174,7 +175,7 @@ func (p *grpcPlugin) CallResource(ctx context.Context, req *backend.CallResource
func (p *grpcPlugin) SubscribeStream(ctx context.Context, request *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
pluginClient, ok := p.getPluginClient()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, plugins.ErrPluginUnavailable
}
return pluginClient.SubscribeStream(ctx, request)
}
@ -182,7 +183,7 @@ func (p *grpcPlugin) SubscribeStream(ctx context.Context, request *backend.Subsc
func (p *grpcPlugin) PublishStream(ctx context.Context, request *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
pluginClient, ok := p.getPluginClient()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, plugins.ErrPluginUnavailable
}
return pluginClient.PublishStream(ctx, request)
}
@ -190,7 +191,7 @@ func (p *grpcPlugin) PublishStream(ctx context.Context, request *backend.Publish
func (p *grpcPlugin) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
pluginClient, ok := p.getPluginClient()
if !ok {
return backendplugin.ErrPluginUnavailable
return plugins.ErrPluginUnavailable
}
return pluginClient.RunStream(ctx, req, sender)
}

View File

@ -3,16 +3,24 @@ package plugins
import "github.com/grafana/grafana/pkg/util/errutil"
var (
errPluginNotRegisteredBase = errutil.NotFound("plugin.notRegistered",
errutil.WithPublicMessage("Plugin not registered"))
// ErrPluginNotRegistered error returned when a plugin is not registered.
ErrPluginNotRegistered = errutil.NotFound("plugin.notRegistered")
// ErrHealthCheckFailed error returned when a plugin health check failed.
ErrHealthCheckFailed = errutil.Internal("plugin.failedHealthCheck")
ErrPluginNotRegistered = errPluginNotRegisteredBase.Errorf("plugin not registered")
errPluginUnavailableBase = errutil.Internal("plugin.unavailable",
errutil.WithPublicMessage("Plugin unavailable"))
// ErrPluginUnavailable error returned when a plugin is unavailable.
ErrPluginUnavailable = errutil.Internal("plugin.unavailable")
ErrPluginUnavailable = errPluginUnavailableBase.Errorf("plugin unavailable")
errMethodNotImplementedBase = errutil.NotFound("plugin.notImplemented",
errutil.WithPublicMessage("Method not implemented"))
// ErrMethodNotImplemented error returned when a plugin method is not implemented.
ErrMethodNotImplemented = errutil.NotImplemented("plugin.notImplemented")
ErrMethodNotImplemented = errMethodNotImplementedBase.Errorf("method not implemented")
// ErrPluginDownstreamError error returned when a plugin request fails.
ErrPluginDownstreamError = errutil.Internal("plugin.downstreamError",
// Exposed as a base error to wrap it with plugin downstream errors.
ErrPluginDownstreamErrorBase = errutil.Internal("plugin.downstreamError",
errutil.WithPublicMessage("An error occurred within the plugin"),
errutil.WithDownstream())
)

View File

@ -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)

View File

@ -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,
},
}

View File

@ -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 {

View File

@ -288,7 +288,7 @@ func (p *Plugin) Target() backendplugin.Target {
func (p *Plugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
pluginClient, ok := p.Client()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, ErrPluginUnavailable
}
return pluginClient.QueryData(ctx, req)
}
@ -296,7 +296,7 @@ func (p *Plugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (
func (p *Plugin) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
pluginClient, ok := p.Client()
if !ok {
return backendplugin.ErrPluginUnavailable
return ErrPluginUnavailable
}
return pluginClient.CallResource(ctx, req, sender)
}
@ -304,7 +304,7 @@ func (p *Plugin) CallResource(ctx context.Context, req *backend.CallResourceRequ
func (p *Plugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
pluginClient, ok := p.Client()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, ErrPluginUnavailable
}
return pluginClient.CheckHealth(ctx, req)
}
@ -312,7 +312,7 @@ func (p *Plugin) CheckHealth(ctx context.Context, req *backend.CheckHealthReques
func (p *Plugin) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
pluginClient, ok := p.Client()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, ErrPluginUnavailable
}
return pluginClient.CollectMetrics(ctx, req)
}
@ -320,7 +320,7 @@ func (p *Plugin) CollectMetrics(ctx context.Context, req *backend.CollectMetrics
func (p *Plugin) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
pluginClient, ok := p.Client()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, ErrPluginUnavailable
}
return pluginClient.SubscribeStream(ctx, req)
}
@ -328,7 +328,7 @@ func (p *Plugin) SubscribeStream(ctx context.Context, req *backend.SubscribeStre
func (p *Plugin) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
pluginClient, ok := p.Client()
if !ok {
return nil, backendplugin.ErrPluginUnavailable
return nil, ErrPluginUnavailable
}
return pluginClient.PublishStream(ctx, req)
}
@ -336,7 +336,7 @@ func (p *Plugin) PublishStream(ctx context.Context, req *backend.PublishStreamRe
func (p *Plugin) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
pluginClient, ok := p.Client()
if !ok {
return backendplugin.ErrPluginUnavailable
return ErrPluginUnavailable
}
return pluginClient.RunStream(ctx, req, sender)
}

View File

@ -880,7 +880,7 @@ func (tp *testPlugin) Target() backendplugin.Target {
}
func (tp *testPlugin) CollectMetrics(_ context.Context, _ *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (tp *testPlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
@ -888,7 +888,7 @@ func (tp *testPlugin) CheckHealth(ctx context.Context, req *backend.CheckHealthR
return tp.CheckHealthHandler.CheckHealth(ctx, req)
}
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (tp *testPlugin) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
@ -896,7 +896,7 @@ func (tp *testPlugin) QueryData(ctx context.Context, req *backend.QueryDataReque
return tp.QueryDataHandler.QueryData(ctx, req)
}
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (tp *testPlugin) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
@ -904,28 +904,28 @@ func (tp *testPlugin) CallResource(ctx context.Context, req *backend.CallResourc
return tp.CallResourceHandler.CallResource(ctx, req, sender)
}
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
func (tp *testPlugin) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
if tp.StreamHandler != nil {
return tp.StreamHandler.SubscribeStream(ctx, req)
}
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (tp *testPlugin) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
if tp.StreamHandler != nil {
return tp.StreamHandler.PublishStream(ctx, req)
}
return nil, backendplugin.ErrMethodNotImplemented
return nil, plugins.ErrMethodNotImplemented
}
func (tp *testPlugin) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
if tp.StreamHandler != nil {
return tp.StreamHandler.RunStream(ctx, req, sender)
}
return backendplugin.ErrMethodNotImplemented
return plugins.ErrMethodNotImplemented
}
func metricRequestWithQueries(t *testing.T, rawQueries ...string) dtos.MetricRequest {