mirror of
https://github.com/grafana/grafana.git
synced 2025-09-21 04:42:54 +08:00
K8s: Folders: fix error conversion (#102737)
This commit is contained in:

committed by
GitHub

parent
c8881c272f
commit
1e4555c79b
@ -205,7 +205,7 @@ Status Codes:
|
|||||||
|
|
||||||
Status Codes:
|
Status Codes:
|
||||||
|
|
||||||
- **200** – Updated
|
- **200** – Updated
|
||||||
- **400** – Errors (invalid json, missing or invalid fields, etc)
|
- **400** – Errors (invalid json, missing or invalid fields, etc)
|
||||||
- **401** – Unauthorized
|
- **401** – Unauthorized
|
||||||
- **403** – Access Denied
|
- **403** – Access Denied
|
||||||
|
@ -44,9 +44,13 @@ func ToFolderErrorResponse(err error) response.Response {
|
|||||||
return response.JSON(http.StatusPreconditionFailed, util.DynMap{"status": "version-mismatch", "message": dashboards.ErrFolderVersionMismatch.Error()})
|
return response.JSON(http.StatusPreconditionFailed, util.DynMap{"status": "version-mismatch", "message": dashboards.ErrFolderVersionMismatch.Error()})
|
||||||
}
|
}
|
||||||
|
|
||||||
// folder errors are wrapped in an error util, so this is the only way of comparing errors
|
if errors.Is(err, folder.ErrMaximumDepthReached) {
|
||||||
if err.Error() == folder.ErrMaximumDepthReached.Error() {
|
return response.JSON(http.StatusBadRequest, util.DynMap{"messageId": "folder.maximum-depth-reached", "message": folder.ErrMaximumDepthReached.Error()})
|
||||||
return response.JSON(http.StatusBadRequest, util.DynMap{"messageId": "folder.maximum-depth-reached", "message": "Maximum nested folder depth reached"})
|
}
|
||||||
|
|
||||||
|
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, "Folder API error", err)
|
||||||
|
@ -10,6 +10,8 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/services/folder"
|
"github.com/grafana/grafana/pkg/services/folder"
|
||||||
"github.com/grafana/grafana/pkg/util"
|
"github.com/grafana/grafana/pkg/util"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestToFolderErrorResponse(t *testing.T) {
|
func TestToFolderErrorResponse(t *testing.T) {
|
||||||
@ -66,13 +68,28 @@ func TestToFolderErrorResponse(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "folder max depth reached",
|
name: "folder max depth reached",
|
||||||
input: folder.ErrMaximumDepthReached,
|
input: folder.ErrMaximumDepthReached,
|
||||||
want: response.JSON(http.StatusBadRequest, util.DynMap{"messageId": "folder.maximum-depth-reached", "message": "Maximum nested folder depth reached"}),
|
want: response.JSON(http.StatusBadRequest, util.DynMap{"messageId": "folder.maximum-depth-reached", "message": folder.ErrMaximumDepthReached.Error()}),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "fallback error",
|
name: "fallback error",
|
||||||
input: errors.New("some error"),
|
input: errors.New("some error"),
|
||||||
want: response.ErrOrFallback(http.StatusInternalServerError, "Folder API error", errors.New("some error")),
|
want: response.ErrOrFallback(http.StatusInternalServerError, "Folder API error", errors.New("some error")),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "kubernetes status error",
|
||||||
|
input: &k8sErrors.StatusError{
|
||||||
|
ErrStatus: metav1.Status{
|
||||||
|
Code: 412,
|
||||||
|
Message: "the folder has been changed by someone else",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
want: response.Error(412, "the folder has been changed by someone else", &k8sErrors.StatusError{
|
||||||
|
ErrStatus: metav1.Status{
|
||||||
|
Code: 412,
|
||||||
|
Message: "the folder has been changed by someone else",
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user