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
+5 -5
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,
+2 -2
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
}
+1 -2
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{
-11
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)
}
-13
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)
}