Skip to content

Modern Docker Management, Designed for Everyone

fix: return 4xx instead of 500 or 200 for not-found and conflict errors (#4506)

José M. Requena Plensefc38c1d8fa5
diff --git a/backend/internal/common/errors.go b/backend/internal/common/errors.go
index 0b84c384b..0ec41ebc5 100644
--- a/backend/internal/common/errors.go
+++ b/backend/internal/common/errors.go
@@ -84,6 +84,7 @@ var (
ErrApnsDeviceConflict = Classify(ErrConflict, errors.New("Mobile push device is already registered"))
ErrApnsRelay = Classify(ErrUnavailable, errors.New("Push relay request failed"))
ErrGlobalVariableConflict = Classify(ErrConflict, errors.New("Global variable already exists"))
+ ErrGlobalVariableUnknownEnvironment = Classify(ErrValidation, errors.New("One or more environments do not exist"))
ErrGlobalVariableScopeRequired = Classify(ErrValidation, errors.New("At least one environment is required when a variable is not scoped to all environments"))
ErrGlobalVariableSecretValueRequired = Classify(ErrValidation, errors.New("A new value is required when making a secret variable readable"))
ErrImageUntagged = Classify(ErrBadRequest, errors.New("image has no tag; only tagged images can be patched"))
diff --git a/backend/internal/environment/handler.go b/backend/internal/environment/handler.go
index 5841c1c33..d38d19f3d 100644
--- a/backend/internal/environment/handler.go
+++ b/backend/internal/environment/handler.go
@@ -514,7 +514,8 @@ func (h *EnvironmentHandler) DeleteEnvironment(ctx context.Context, input *Delet
userID, username := currentUserRefs(ctx)
if err := h.environmentService.DeleteEnvironment(ctx, input.ID, userID, username); err != nil {
- return nil, huma.Error500InternalServerError("Failed to delete environment: " + err.Error())
+ apiErr := common.ToAPIError(err)
+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to delete environment: "+apiErr.Message)
}
return handlerutil.MessageOutput("Environment deleted successfully", ""), nil
@@ -559,6 +560,9 @@ func (h *EnvironmentHandler) TestConnection(ctx context.Context, input *TestConn
// UpdateHeartbeat updates the heartbeat for an environment.
func (h *EnvironmentHandler) UpdateHeartbeat(ctx context.Context, input *UpdateHeartbeatInput) (*handlerutil.Out[base.MessageResponse], error) {
if err := h.environmentService.UpdateEnvironmentHeartbeat(ctx, input.ID); err != nil {
+ if errors.Is(err, ErrEnvironmentNotFound) {
+ return nil, huma.Error404NotFound("Environment not found")
+ }
return nil, huma.Error500InternalServerError("Failed to update heartbeat")
}
diff --git a/backend/internal/environment/service.go b/backend/internal/environment/service.go
index f84c522d2..a689606f7 100644
--- a/backend/internal/environment/service.go
+++ b/backend/internal/environment/service.go
@@ -96,7 +96,7 @@ const (
var (
ErrEnvironmentAccessTokenRequired = errors.New("environment access token required")
ErrInvalidEnvironmentAccessToken = errors.New("invalid environment access token")
- ErrEnvironmentNotFound = errors.New("environment not found")
+ ErrEnvironmentNotFound = common.Classify(common.ErrNotFound, errors.New("environment not found"))
)
// VariableSyncer pushes the effective global-variable set to one environment.
@@ -1370,9 +1370,12 @@ func (s *EnvironmentService) UpdateEnvironmentHeartbeat(ctx context.Context, id
if result.RowsAffected > 0 {
s.NotifyRuntimeStateChanged()
+ return nil
}
- return nil
+ // No row changed: the heartbeat was throttled or the environment does not exist.
+ _, err := s.GetEnvironmentByIDCached(ctx, id)
+ return err
}
// UpdateEnvironmentConnectionState records an edge tunnel connect or disconnect
diff --git a/backend/internal/notification/handler.go b/backend/internal/notification/handler.go
index 8fe794c90..276981f0d 100644
--- a/backend/internal/notification/handler.go
+++ b/backend/internal/notification/handler.go
@@ -183,7 +183,8 @@ func (h *NotificationHandler) TestNotification(ctx context.Context, input *TestN
warning, err := h.notificationService.TestNotification(ctx, input.EnvironmentID, provider, testType)
if err != nil {
- return nil, huma.Error500InternalServerError("Failed to send test notification: " + err.Error())
+ apiErr := common.ToAPIError(err)
+ return nil, huma.NewError(apiErr.HTTPStatus(), "Failed to send test notification: "+apiErr.Message)
}
return &handlerutil.Out[notification.TestResponse]{
diff --git a/backend/internal/registry/service.go b/backend/internal/registry/service.go
index 2ca0572da..1b215ea79 100644
--- a/backend/internal/registry/service.go
+++ b/backend/internal/registry/service.go
@@ -415,8 +415,12 @@ func (s *ContainerRegistryService) UpdateRegistry(ctx context.Context, id string
}
func (s *ContainerRegistryService) DeleteRegistry(ctx context.Context, id string) error {
- if err := s.db.WithContext(ctx).Where("id = ?", id).Delete(&ContainerRegistry{}).Error; err != nil {
- return fmt.Errorf("failed to delete container registry: %w", err)
+ result := s.db.WithContext(ctx).Where("id = ?", id).Delete(&ContainerRegistry{})
+ if result.Error != nil {
+ return fmt.Errorf("failed to delete container registry: %w", result.Error)
+ }
+ if result.RowsAffected == 0 {
+ return common.ErrContainerRegistryNotFound
}
return nil
}
@@ -1124,7 +1128,8 @@ func (s *ContainerRegistryService) SyncRegistries(ctx context.Context, syncItems
if slices.ContainsFunc(syncItems, func(item containerregistry.Sync) bool { return item.ID == id }) {
continue
}
- if deleteErr := s.DeleteRegistry(ctx, id); deleteErr != nil {
+ // Already gone is the desired state for sync.
+ if deleteErr := s.DeleteRegistry(ctx, id); deleteErr != nil && !errors.Is(deleteErr, common.ErrNotFound) {
return deleteErr
}
}
diff --git a/backend/internal/variable/handler.go b/backend/internal/variable/handler.go
index f4ecb62e1..785eeedb6 100644
--- a/backend/internal/variable/handler.go
+++ b/backend/internal/variable/handler.go
@@ -178,7 +178,8 @@ func variableMutationHTTPErrorInternal(err error) error {
switch {
case errors.Is(err, common.ErrInvalidEnvKey),
errors.Is(err, common.ErrGlobalVariableSecretValueRequired),
- errors.Is(err, common.ErrGlobalVariableScopeRequired):
+ errors.Is(err, common.ErrGlobalVariableScopeRequired),
+ errors.Is(err, common.ErrGlobalVariableUnknownEnvironment):
return huma.Error400BadRequest(err.Error())
case errors.Is(err, common.ErrGlobalVariableNotFound):
return huma.Error404NotFound(err.Error())
diff --git a/backend/internal/variable/service.go b/backend/internal/variable/service.go
index fe5b7a74f..47f24d25a 100644
--- a/backend/internal/variable/service.go
+++ b/backend/internal/variable/service.go
@@ -741,7 +741,10 @@ func (s *VariableService) normalizeScopeInternal(ctx context.Context, allEnviron
return nil, fmt.Errorf("failed to update global variables: %w", err)
}
if count != int64(len(unique)) {
- return nil, errors.New("failed to update global variables: one or more environments do not exist")
+ return nil, common.Classify(
+ common.ErrGlobalVariableUnknownEnvironment,
+ errors.New("failed to update global variables: one or more environments do not exist"),
+ )
}
return unique, nil
}
diff --git a/backend/internal/volume/children/backup/handler.go b/backend/internal/volume/children/backup/handler.go
index 5335f0bae..a8e332b3a 100644
--- a/backend/internal/volume/children/backup/handler.go
+++ b/backend/internal/volume/children/backup/handler.go
@@ -179,6 +179,9 @@ func (h *Handler) DeleteBackup(ctx context.Context, input *DeleteBackupInput) (*
}, func(runtimeCtx context.Context) error {
return h.service.DeleteBackup(runtimeCtx, input.BackupID, user)
})
+ if errors.Is(err, h.service.deps.AlreadyRunning) {
+ return nil, huma.Error409Conflict(err.Error())
+ }
if err != nil {
return nil, huma.Error500InternalServerError(err.Error())
}
@@ -228,6 +231,9 @@ func (h *Handler) UploadBackup(ctx context.Context, input *UploadBackupInput) (*
uploaded, uploadErr = h.service.UploadBackup(runtimeCtx, input.BackupID, input.Body.S3DestinationID)
return uploadErr
})
+ if errors.Is(err, h.service.deps.AlreadyRunning) {
+ return nil, huma.Error409Conflict(err.Error())
+ }
if err != nil {
return nil, huma.Error500InternalServerError(err.Error())
}