fix: improve api error handling for dashboards and folders (#111831)
This commit is contained in:
@@ -3,6 +3,7 @@ package apierrors
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
|
||||
@@ -17,45 +18,49 @@ import (
|
||||
|
||||
// ToFolderErrorResponse returns a different response status according to the folder error type
|
||||
func ToFolderErrorResponse(err error) response.Response {
|
||||
// --- Dashboard errors ---
|
||||
var dashboardErr dashboardaccess.DashboardErr
|
||||
if ok := errors.As(err, &dashboardErr); ok {
|
||||
return response.Error(dashboardErr.StatusCode, err.Error(), err)
|
||||
}
|
||||
|
||||
// --- 400 Bad Request ---
|
||||
if errors.Is(err, dashboards.ErrFolderTitleEmpty) ||
|
||||
errors.Is(err, dashboards.ErrDashboardTypeMismatch) ||
|
||||
errors.Is(err, dashboards.ErrDashboardInvalidUid) ||
|
||||
errors.Is(err, dashboards.ErrDashboardUidTooLong) {
|
||||
errors.Is(err, dashboards.ErrDashboardUidTooLong) ||
|
||||
errors.Is(err, folder.ErrFolderCannotBeParentOfItself) {
|
||||
return response.Error(http.StatusBadRequest, err.Error(), nil)
|
||||
}
|
||||
|
||||
// --- 403 Forbidden ---
|
||||
if errors.Is(err, dashboards.ErrFolderAccessDenied) {
|
||||
return response.Error(http.StatusForbidden, "Access denied", err)
|
||||
}
|
||||
|
||||
// --- 404 Not Found ---
|
||||
if errors.Is(err, dashboards.ErrFolderNotFound) {
|
||||
return response.JSON(http.StatusNotFound, util.DynMap{"status": "not-found", "message": dashboards.ErrFolderNotFound.Error()})
|
||||
}
|
||||
|
||||
// --- 409 Conflict ---
|
||||
if errors.Is(err, dashboards.ErrFolderWithSameUIDExists) {
|
||||
return response.Error(http.StatusConflict, err.Error(), nil)
|
||||
}
|
||||
|
||||
// --- 412 Precondition Failed ---
|
||||
if errors.Is(err, dashboards.ErrFolderVersionMismatch) ||
|
||||
k8sErrors.IsAlreadyExists(err) {
|
||||
return response.JSON(http.StatusPreconditionFailed, util.DynMap{"status": "version-mismatch", "message": dashboards.ErrFolderVersionMismatch.Error()})
|
||||
}
|
||||
|
||||
if errors.Is(err, folder.ErrMaximumDepthReached) {
|
||||
return response.JSON(http.StatusBadRequest, util.DynMap{"messageId": "folder.maximum-depth-reached", "message": folder.ErrMaximumDepthReached.Error()})
|
||||
}
|
||||
|
||||
// --- Kubernetes status errors ---
|
||||
var statusErr *k8sErrors.StatusError
|
||||
if errors.As(err, &statusErr) {
|
||||
return response.Error(int(statusErr.ErrStatus.Code), statusErr.ErrStatus.Message, err)
|
||||
}
|
||||
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, "Folder API error", err)
|
||||
return response.ErrOrFallback(http.StatusInternalServerError, fmt.Sprintf("Folder API error: %s", err.Error()), err)
|
||||
}
|
||||
|
||||
func ToFolderStatusError(err error) k8sErrors.StatusError {
|
||||
|
||||
Reference in New Issue
Block a user