Skip to content

Modern Docker Management, Designed for Everyone

fix: map Docker not-found and conflict errors to 4xx on more routes (#4598)

José M. Requena Plensdf92fd30f4ba
diff --git a/backend/internal/container/handler.go b/backend/internal/container/handler.go
index 654c9bd17..ea64ca438 100644
--- a/backend/internal/container/handler.go
+++ b/backend/internal/container/handler.go
@@ -547,7 +547,7 @@ func (h *ContainerHandler) CommitContainer(ctx context.Context, input *CommitCon
out, err := h.containerService.CommitContainer(ctx, input.ContainerID, input.Body, *user)
if err != nil {
- return nil, huma.Error500InternalServerError(fmt.Sprintf("failed to commit container: %v", err))
+ return nil, huma.NewError(common.ToAPIError(common.ClassifyDockerError(err)).HTTPStatus(), fmt.Sprintf("failed to commit container: %v", err))
}
return &handlerutil.Out[container.CommitResult]{
diff --git a/backend/internal/image/children/patch/service.go b/backend/internal/image/children/patch/service.go
index b1952ccbc..26d7a6a84 100644
--- a/backend/internal/image/children/patch/service.go
+++ b/backend/internal/image/children/patch/service.go
@@ -179,7 +179,7 @@ func (s *Service) startPatch(
return dockerClient.ImageInspect(ctx, imageID)
})
if err != nil {
- return nil, nil, fmt.Errorf("failed to inspect image: %w", err)
+ return nil, nil, common.ClassifyDockerError(fmt.Errorf("failed to inspect image: %w", err))
}
if len(imageInspect.RepoTags) == 0 {
return nil, nil, common.ErrImageUntagged
diff --git a/backend/internal/image/handler.go b/backend/internal/image/handler.go
index f914a5ba2..668dc5074 100644
--- a/backend/internal/image/handler.go
+++ b/backend/internal/image/handler.go
@@ -213,7 +213,7 @@ func (h *ImageHandler) TagImage(ctx context.Context, input *TagImageInput) (*han
}
if tagImageErr := h.imageService.TagImage(ctx, imageName, input.Body, *user); tagImageErr != nil {
- return nil, huma.Error500InternalServerError(fmt.Sprintf("failed to tag image: %v", tagImageErr))
+ return nil, huma.NewError(common.ToAPIError(common.ClassifyDockerError(tagImageErr)).HTTPStatus(), fmt.Sprintf("failed to tag image: %v", tagImageErr))
}
return handlerutil.MessageOutput("Image tagged successfully", ""), nil
@@ -228,7 +228,7 @@ func (h *ImageHandler) GetImageHistory(ctx context.Context, input *GetImageHisto
history, err := h.imageService.GetImageHistory(ctx, imageName)
if err != nil {
- return nil, huma.Error500InternalServerError(fmt.Sprintf("failed to get image history: %v", err))
+ return nil, huma.NewError(common.ToAPIError(common.ClassifyDockerError(err)).HTTPStatus(), fmt.Sprintf("failed to get image history: %v", err))
}
if history == nil {
history = []image.HistoryItem{}
@@ -272,7 +272,7 @@ func (h *ImageHandler) ExportImage(ctx context.Context, input *ExportImageInput)
reader, err := h.imageService.ExportImage(ctx, imageName)
if err != nil {
- return nil, huma.Error500InternalServerError(fmt.Sprintf("failed to export image: %v", err))
+ return nil, huma.NewError(common.ToAPIError(common.ClassifyDockerError(err)).HTTPStatus(), fmt.Sprintf("failed to export image: %v", err))
}
return &huma.StreamResponse{
diff --git a/backend/internal/imageupdate/handler.go b/backend/internal/imageupdate/handler.go
index e02d2773c..7249760be 100644
--- a/backend/internal/imageupdate/handler.go
+++ b/backend/internal/imageupdate/handler.go
@@ -10,6 +10,7 @@ import (
"github.com/getarcaneapp/arcane/types/v2/imageupdate"
"go.getarcane.app/kit/pkg"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
)
@@ -76,7 +77,7 @@ func (h *ImageUpdateHandler) CheckImageUpdateByID(ctx context.Context, input *Ch
runtimeCtx := utils.ActivityRuntimeContext(ctx, h.appCtx)
result, err := h.imageUpdateService.CheckImageUpdateByID(runtimeCtx, input.ImageID)
if err != nil {
- return nil, huma.Error500InternalServerError("Failed to check image update: " + err.Error())
+ return nil, huma.NewError(common.ToAPIError(err).HTTPStatus(), "Failed to check image update: "+err.Error())
}
return &handlerutil.Out[imageupdate.Response]{
diff --git a/backend/internal/imageupdate/service.go b/backend/internal/imageupdate/service.go
index 66db06df9..ce0efd626 100644
--- a/backend/internal/imageupdate/service.go
+++ b/backend/internal/imageupdate/service.go
@@ -535,7 +535,7 @@ func (s *ImageUpdateService) getImageRefByID(ctx context.Context, imageID string
return imageRef, nil
}
- return "", fmt.Errorf("image not found: no local image or running container found for %s", imageID)
+ return "", common.Classify(common.ErrNotFound, fmt.Errorf("image not found: no local image or running container found for %s", imageID))
}
func (s *ImageUpdateService) getAllImageRefs(ctx context.Context, limit int) ([]string, error) {
diff --git a/backend/internal/network/handler.go b/backend/internal/network/handler.go
index c07c4bbf9..cec4b84ab 100644
--- a/backend/internal/network/handler.go
+++ b/backend/internal/network/handler.go
@@ -171,7 +171,7 @@ func (h *NetworkHandler) CreateNetwork(ctx context.Context, input *CreateNetwork
return createErr
})
if err != nil {
- return nil, huma.Error500InternalServerError("Failed to create network: " + err.Error())
+ return nil, huma.NewError(common.ToAPIError(common.ClassifyDockerError(err)).HTTPStatus(), "Failed to create network: "+err.Error())
}
out, err := mapping.MapOne[network.CreateResponse, networktypes.CreateResponse](*response)
diff --git a/backend/internal/swarm/handler.go b/backend/internal/swarm/handler.go
index 1aff19f35..768b66d5c 100644
--- a/backend/internal/swarm/handler.go
+++ b/backend/internal/swarm/handler.go
@@ -425,7 +425,7 @@ func mapSwarmServiceErrorInternal(err error, fallback string) error {
if errdefs.IsInvalidArgument(err) {
return huma.Error400BadRequest(err.Error())
}
- if errdefs.IsConflict(err) {
+ if errdefs.IsConflict(err) || errors.Is(err, common.ErrConflict) {
return huma.Error409Conflict(err.Error())
}
if remoteStatus, ok := errors.AsType[*remenv.StatusError](err); ok && remoteStatus.StatusCode >= 400 && remoteStatus.StatusCode <= 599 {
diff --git a/backend/internal/swarm/service.go b/backend/internal/swarm/service.go
index da15018f7..6aa38453c 100644
--- a/backend/internal/swarm/service.go
+++ b/backend/internal/swarm/service.go
@@ -118,6 +118,11 @@ func (s *SwarmService) GetSwarmInfo(ctx context.Context) (*swarmtypes.SwarmInfo,
}
func (s *SwarmService) InitSwarm(ctx context.Context, req swarmtypes.SwarmInitRequest) (*swarmtypes.SwarmInitResponse, error) {
+ if !req.ForceNewCluster {
+ if err := s.ensureSwarmInactive(ctx); err != nil {
+ return nil, err
+ }
+ }
spec, err := decodeSwarmSpec(req.Spec)
if err != nil {
return nil, err
@@ -156,6 +161,9 @@ func (s *SwarmService) InitSwarm(ctx context.Context, req swarmtypes.SwarmInitRe
}
func (s *SwarmService) JoinSwarm(ctx context.Context, req swarmtypes.SwarmJoinRequest) error {
+ if err := s.ensureSwarmInactive(ctx); err != nil {
+ return err
+ }
if _, swarmJoinErr := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.SwarmJoinResult, error) {
return dockerClient.SwarmJoin(ctx, client.SwarmJoinOptions{
ListenAddr: cmp.Or(strings.TrimSpace(req.ListenAddr), defaultSwarmListenAddr),
@@ -414,6 +422,19 @@ func (s *SwarmService) ensureSwarmActive(ctx context.Context) (system.Info, erro
return info, nil
}
+func (s *SwarmService) ensureSwarmInactive(ctx context.Context) error {
+ info, err := s.getDockerInfo(ctx)
+ if err != nil {
+ return err
+ }
+ switch info.Swarm.LocalNodeState {
+ case swarm.LocalNodeStateActive, swarm.LocalNodeStatePending, swarm.LocalNodeStateLocked:
+ return common.Classify(common.ErrConflict, errors.New("this node is already part of a swarm"))
+ default:
+ return nil
+ }
+}
+
func (s *SwarmService) getDockerInfo(ctx context.Context) (system.Info, error) {
infoResult, err := s.dockerService.Call(ctx, func(ctx context.Context, dockerClient *client.Client) (client.SystemInfoResult, error) {
return dockerClient.Info(ctx, client.InfoOptions{})
diff --git a/backend/internal/updater/service.go b/backend/internal/updater/service.go
index af24dbcba..6973b07c9 100644
--- a/backend/internal/updater/service.go
+++ b/backend/internal/updater/service.go
@@ -733,6 +733,9 @@ func (s *UpdaterService) runSingleContainerUpdate(ctx context.Context, container
out = resultFromModule(moduleResult)
s.logResultItems(ctx, out)
}
+ if errors.Is(err, updater.ErrContainerNotFound) {
+ err = common.Classify(common.ErrNotFound, err)
+ }
return out, err
}
diff --git a/backend/internal/volume/handler.go b/backend/internal/volume/handler.go
index f75c04811..054c81a0e 100644
--- a/backend/internal/volume/handler.go
+++ b/backend/internal/volume/handler.go
@@ -214,7 +214,7 @@ func (h *VolumeHandler) CreateVolume(ctx context.Context, input *CreateVolumeInp
return createErr
})
if err != nil {
- return nil, huma.Error500InternalServerError("Failed to create volume: " + err.Error())
+ return nil, huma.NewError(common.ToAPIError(common.ClassifyDockerError(err)).HTTPStatus(), "Failed to create volume: "+err.Error())
}
response.ActivityID = mo.EmptyableToOption(strings.TrimSpace(activityID)).ToPointer()
@@ -315,7 +315,7 @@ func (h *VolumeHandler) RemoveVolume(ctx context.Context, input *RemoveVolumeInp
return h.volumeService.DeleteVolume(runtimeCtx, input.VolumeName, input.Force, *user)
})
if err != nil {
- return nil, huma.Error500InternalServerError("Failed to delete volume: " + err.Error())
+ return nil, huma.NewError(common.ToAPIError(common.ClassifyDockerError(err)).HTTPStatus(), "Failed to delete volume: "+err.Error())
}
return handlerutil.MessageOutput("Volume removed successfully", activityID), nil
@@ -358,7 +358,7 @@ func (h *VolumeHandler) PruneVolumes(ctx context.Context, input *PruneVolumesInp
func (h *VolumeHandler) GetVolumeUsage(ctx context.Context, input *GetVolumeUsageInput) (*handlerutil.Out[VolumeUsageResponse], error) {
inUse, containers, err := h.volumeService.GetVolumeUsage(ctx, input.VolumeName)
if err != nil {
- return nil, huma.Error500InternalServerError("Failed to get volume usage: " + err.Error())
+ return nil, huma.NewError(common.ToAPIError(common.ClassifyDockerError(err)).HTTPStatus(), "Failed to get volume usage: "+err.Error())
}
return &handlerutil.Out[VolumeUsageResponse]{
diff --git a/backend/internal/vulnerability/children/scan/handler.go b/backend/internal/vulnerability/children/scan/handler.go
index 20c0671cb..6a6d07aab 100644
--- a/backend/internal/vulnerability/children/scan/handler.go
+++ b/backend/internal/vulnerability/children/scan/handler.go
@@ -42,7 +42,7 @@ func (h *Handler) ScanImage(ctx context.Context, input *ScanImageInput) (*handle
if errors.Is(err, common.ErrFeatureDisabled) {
return nil, handlerutil.FeatureDisabledError(features.VulnerabilityManagement)
}
- return nil, huma.Error500InternalServerError("Failed to scan image for vulnerabilities: " + err.Error())
+ return nil, huma.NewError(common.ToAPIError(err).HTTPStatus(), "Failed to scan image for vulnerabilities: "+err.Error())
}
return &handlerutil.Out[vulnerability.ScanResult]{
diff --git a/backend/internal/vulnerability/children/scan/service.go b/backend/internal/vulnerability/children/scan/service.go
index 79d8694fd..2a69687f5 100644
--- a/backend/internal/vulnerability/children/scan/service.go
+++ b/backend/internal/vulnerability/children/scan/service.go
@@ -185,7 +185,7 @@ func (s *Service) ScanImage(ctx context.Context, envID, imageID string, user use
return cli.ImageInspect(ctx, imageID)
})
if err != nil {
- return nil, fmt.Errorf("failed to inspect image: %w", err)
+ return nil, common.ClassifyDockerError(fmt.Errorf("failed to inspect image: %w", err))
}
imageName := slices.Concat(imageInspect.RepoTags, imageInspect.RepoDigests, []string{imageID})[0]
diff --git a/backend/internal/webhook/handler.go b/backend/internal/webhook/handler.go
index 9dcc11a63..25888dce9 100644
--- a/backend/internal/webhook/handler.go
+++ b/backend/internal/webhook/handler.go
@@ -9,6 +9,7 @@ import (
"github.com/getarcaneapp/arcane/types/v2/user"
"github.com/getarcaneapp/arcane/types/v2/webhook"
+ "github.com/getarcaneapp/arcane/backend/v2/internal/common"
"github.com/getarcaneapp/arcane/backend/v2/internal/middleware"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/handlerutil"
"github.com/getarcaneapp/arcane/backend/v2/pkg/utils/userctx"
@@ -88,6 +89,9 @@ func (h *WebhookHandler) CreateWebhook(ctx context.Context, input *CreateWebhook
if errors.Is(err, ErrWebhookInvalidAction) {
return nil, huma.Error400BadRequest("invalid action type for target type")
}
+ if errors.Is(err, common.ErrBadRequest) {
+ return nil, huma.Error400BadRequest(err.Error())
+ }
return nil, huma.Error500InternalServerError("failed to create webhook")
}
diff --git a/backend/internal/webhook/service.go b/backend/internal/webhook/service.go
index 2dbc6808a..2357e785b 100644
--- a/backend/internal/webhook/service.go
+++ b/backend/internal/webhook/service.go
@@ -16,6 +16,7 @@ import (
"sync"
"time"
+ "github.com/containerd/errdefs"
"github.com/getarcaneapp/arcane/types/v2"
"github.com/getarcaneapp/arcane/types/v2/base"
updatertypes "github.com/getarcaneapp/arcane/types/v2/updater"
@@ -768,6 +769,9 @@ func (s *WebhookService) resolveContainerWebhookTargetRefInternal(ctx context.Co
}
containerName, err := s.containerService.GetContainerNameByReference(ctx, targetID)
+ if errdefs.IsNotFound(err) {
+ return "", common.Classify(common.ErrBadRequest, fmt.Errorf("target container not found: %w", err))
+ }
if err != nil {
return "", fmt.Errorf("failed to resolve container target reference: %w", err)
}