2022-01-28 17:28:33 +08:00
|
|
|
package apierrors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2025-03-05 07:11:42 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver"
|
2022-06-30 21:31:54 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards"
|
2025-03-26 20:26:14 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/dashboards/dashboardaccess"
|
2023-09-11 19:59:24 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore"
|
2022-01-28 17:28:33 +08:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2025-03-05 07:11:42 +08:00
|
|
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
2022-01-28 17:28:33 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// ToDashboardErrorResponse returns a different response status according to the dashboard error type
|
2023-09-11 19:59:24 +08:00
|
|
|
func ToDashboardErrorResponse(ctx context.Context, pluginStore pluginstore.Store, err error) response.Response {
|
2025-10-01 15:54:14 +08:00
|
|
|
// --- Dashboard errors ---
|
2025-03-26 20:26:14 +08:00
|
|
|
var dashboardErr dashboardaccess.DashboardErr
|
2025-10-01 15:54:14 +08:00
|
|
|
if errors.As(err, &dashboardErr) {
|
2022-01-28 17:28:33 +08:00
|
|
|
if body := dashboardErr.Body(); body != nil {
|
|
|
|
return response.JSON(dashboardErr.StatusCode, body)
|
|
|
|
}
|
|
|
|
if dashboardErr.StatusCode != http.StatusBadRequest {
|
|
|
|
return response.Error(dashboardErr.StatusCode, dashboardErr.Error(), err)
|
|
|
|
}
|
|
|
|
return response.Error(dashboardErr.StatusCode, dashboardErr.Error(), nil)
|
|
|
|
}
|
|
|
|
|
2025-10-01 15:54:14 +08:00
|
|
|
// --- 400 Bad Request ---
|
2022-06-30 21:31:54 +08:00
|
|
|
if errors.Is(err, dashboards.ErrFolderNotFound) {
|
2022-01-28 17:28:33 +08:00
|
|
|
return response.Error(http.StatusBadRequest, err.Error(), nil)
|
|
|
|
}
|
|
|
|
|
2022-06-30 21:31:54 +08:00
|
|
|
var pluginErr dashboards.UpdatePluginDashboardError
|
2025-10-01 15:54:14 +08:00
|
|
|
if errors.As(err, &pluginErr) {
|
2022-01-28 17:28:33 +08:00
|
|
|
message := fmt.Sprintf("The dashboard belongs to plugin %s.", pluginErr.PluginId)
|
|
|
|
// look up plugin name
|
|
|
|
if plugin, exists := pluginStore.Plugin(ctx, pluginErr.PluginId); exists {
|
|
|
|
message = fmt.Sprintf("The dashboard belongs to plugin %s.", plugin.Name)
|
|
|
|
}
|
2025-10-01 15:54:14 +08:00
|
|
|
// --- 412 Precondition Failed ---
|
2022-01-28 17:28:33 +08:00
|
|
|
return response.JSON(http.StatusPreconditionFailed, util.DynMap{"status": "plugin-dashboard", "message": message})
|
|
|
|
}
|
|
|
|
|
2025-10-01 15:54:14 +08:00
|
|
|
// --- 413 Payload Too Large ---
|
2025-03-05 07:11:42 +08:00
|
|
|
if apierrors.IsRequestEntityTooLargeError(err) {
|
|
|
|
return response.Error(http.StatusRequestEntityTooLarge, fmt.Sprintf("Dashboard is too large, max is %d MB", apiserver.MaxRequestBodyBytes/1024/1024), err)
|
|
|
|
}
|
|
|
|
|
2025-10-01 15:54:14 +08:00
|
|
|
// --- Kubernetes status errors ---
|
2025-03-31 21:34:54 +08:00
|
|
|
var statusErr *apierrors.StatusError
|
|
|
|
if errors.As(err, &statusErr) {
|
|
|
|
return response.Error(int(statusErr.ErrStatus.Code), statusErr.ErrStatus.Message, err)
|
|
|
|
}
|
|
|
|
|
2025-10-01 15:54:14 +08:00
|
|
|
return response.ErrOrFallback(http.StatusInternalServerError, fmt.Sprintf("Dashboard API error: %s", err.Error()), err)
|
2022-01-28 17:28:33 +08:00
|
|
|
}
|