Skip to content

Modern Docker Management, Designed for Everyone

fix: return 404 for manager-side routes of an unknown environment (#4597)

José M. Requena Plens91eef67d8c55
diff --git a/backend/internal/environment/handler.go b/backend/internal/environment/handler.go
index d38d19f3d..34c44dfbc 100644
--- a/backend/internal/environment/handler.go
+++ b/backend/internal/environment/handler.go
@@ -540,6 +540,9 @@ func (h *EnvironmentHandler) TestConnection(ctx context.Context, input *TestConn
},
}, huma.NewError(apiErr.HTTPStatus(), apiErr.Message)
}
+ if errors.Is(err, ErrEnvironmentNotFound) {
+ return nil, huma.Error404NotFound(err.Error())
+ }
if err != nil {
return &handlerutil.Out[environment.Test]{
Body: base.ApiResponse[environment.Test]{
@@ -603,7 +606,7 @@ func (h *EnvironmentHandler) SyncEnvironment(ctx context.Context, input *SyncEnv
runtimeCtx := utils.ActivityRuntimeContext(ctx, h.appCtx)
activityID, err := h.environmentService.SyncResourcesToEnvironment(runtimeCtx, input.ID, user, h.activityService)
if err != nil {
- return nil, huma.Error500InternalServerError(err.Error())
+ return nil, huma.NewError(common.ToAPIError(err).HTTPStatus(), err.Error())
}
return handlerutil.MessageOutput("Environment synced successfully", activityID), nil
diff --git a/backend/internal/environment/service.go b/backend/internal/environment/service.go
index a28bb1eb0..ae1dba2db 100644
--- a/backend/internal/environment/service.go
+++ b/backend/internal/environment/service.go
@@ -773,6 +773,9 @@ func (s *EnvironmentService) GetEnabledRegistryCredentials(ctx context.Context)
// SyncResourcesToEnvironment tracks a manual sync and attempts every resource group.
func (s *EnvironmentService) SyncResourcesToEnvironment(ctx context.Context, environmentID string, user *usertypes.Actor, activityService activity.Service) (string, error) {
+ if _, err := s.GetEnvironmentByID(ctx, environmentID); err != nil {
+ return "", err
+ }
return activity.RunHandlerActivity(ctx, activityService, activity.HandlerOptions{
EnvironmentID: environmentID,
Type: activitytypes.TypeResourceAction,
diff --git a/backend/internal/swarm/handler.go b/backend/internal/swarm/handler.go
index 997445b9d..1aff19f35 100644
--- a/backend/internal/swarm/handler.go
+++ b/backend/internal/swarm/handler.go
@@ -419,7 +419,7 @@ func mapSwarmServiceErrorInternal(err error, fallback string) error {
if errors.Is(err, common.ErrBadRequest) {
return huma.Error400BadRequest(err.Error())
}
- if errdefs.IsNotFound(err) {
+ if errdefs.IsNotFound(err) || errors.Is(err, common.ErrNotFound) {
return huma.Error404NotFound(err.Error())
}
if errdefs.IsInvalidArgument(err) {
diff --git a/backend/internal/volume/children/backup/children/policies/handler.go b/backend/internal/volume/children/backup/children/policies/handler.go
index aed3da381..b00f87b57 100644
--- a/backend/internal/volume/children/backup/children/policies/handler.go
+++ b/backend/internal/volume/children/backup/children/policies/handler.go
@@ -129,6 +129,9 @@ func (h *Handler) UpdateBackupPolicy(ctx context.Context, input *UpdateVolumeBac
)
if proxyErr != nil {
errorStatus = http.StatusBadGateway
+ if statusErr, ok := errors.AsType[huma.StatusError](proxyErr); ok && statusErr.GetStatus() == http.StatusNotFound {
+ errorStatus = http.StatusNotFound
+ }
}
return proxyErr
}
@@ -140,8 +143,8 @@ func (h *Handler) UpdateBackupPolicy(ctx context.Context, input *UpdateVolumeBac
if errorStatus == http.StatusInternalServerError {
return nil, huma.Error500InternalServerError(err.Error())
}
- if errorStatus == http.StatusBadGateway {
- return nil, huma.NewError(http.StatusBadGateway, err.Error())
+ if errorStatus == http.StatusBadGateway || errorStatus == http.StatusNotFound {
+ return nil, huma.NewError(errorStatus, err.Error())
}
return nil, huma.Error400BadRequest(err.Error())
}
diff --git a/backend/pkg/utils/handlerutil/remote_proxy.go b/backend/pkg/utils/handlerutil/remote_proxy.go
index a3bf2b95a..7e691912f 100644
--- a/backend/pkg/utils/handlerutil/remote_proxy.go
+++ b/backend/pkg/utils/handlerutil/remote_proxy.go
@@ -8,6 +8,7 @@ import (
"github.com/danielgtaylor/huma/v2"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/pkg/remenv"
)
@@ -63,5 +64,5 @@ func TranslateRemoteProxyError(err error) error {
return huma.Error500InternalServerError("failed to decode environment response: " + decodeErr.Error())
}
- return huma.Error500InternalServerError("failed to proxy request to environment: " + err.Error())
+ return huma.NewError(common.ToAPIError(err).HTTPStatus(), "failed to proxy request to environment: "+err.Error())
}