diff --git a/pkg/adapter/config/kubernetes/errorhandling.go b/pkg/adapter/config/kubernetes/errorhandling.go index 80fa767..20da9de 100644 --- a/pkg/adapter/config/kubernetes/errorhandling.go +++ b/pkg/adapter/config/kubernetes/errorhandling.go @@ -10,7 +10,7 @@ func mapToBlueprintError(err error) error { if liberrors.IsNotFoundError(err) { return domainservice.NewNotFoundError(err, "could not find config. Check if your ecosystem is ready for operation") } else if liberrors.IsConflictError(err) { - return domainservice.NewInternalError(err, "could not update config due to conflicting changes") + return domainservice.NewConflictError(err, "could not update config due to conflicting changes") } else if liberrors.IsConnectionError(err) { return domainservice.NewInternalError(err, "could not load/update config due to connection problems") } else if liberrors.IsAlreadyExistsError(err) { diff --git a/pkg/adapter/kubernetes/blueprintcr/blueprintSpecCRRepository.go b/pkg/adapter/kubernetes/blueprintcr/blueprintSpecCRRepository.go index efb3f7a..de41973 100644 --- a/pkg/adapter/kubernetes/blueprintcr/blueprintSpecCRRepository.go +++ b/pkg/adapter/kubernetes/blueprintcr/blueprintSpecCRRepository.go @@ -149,10 +149,7 @@ func (repo *blueprintSpecRepo) Update(ctx context.Context, spec *domain.Blueprin CRAfterUpdate, err := repo.blueprintClient.Update(ctx, &updatedBlueprint, metav1.UpdateOptions{}) if err != nil { if k8sErrors.IsConflict(err) { - return &domainservice.ConflictError{ - WrappedError: err, - Message: fmt.Sprintf("cannot update blueprint CR %q as it was modified in the meantime", spec.Id), - } + return domainservice.NewConflictError(err, "cannot update blueprint CR %q as it was modified in the meantime", spec.Id) } return domainservice.NewInternalError(err, "cannot update blueprint CR %q", spec.Id) } @@ -167,10 +164,7 @@ func (repo *blueprintSpecRepo) Update(ctx context.Context, spec *domain.Blueprin CRAfterUpdate, err = repo.blueprintClient.UpdateStatus(ctx, CRAfterUpdate, metav1.UpdateOptions{}) if err != nil { if k8sErrors.IsConflict(err) { - return &domainservice.ConflictError{ - WrappedError: err, - Message: fmt.Sprintf("cannot update blueprint CR status %q as it was modified in the meantime", spec.Id), - } + return domainservice.NewConflictError(err, "cannot update blueprint CR status %q as it was modified in the meantime", spec.Id) } return domainservice.NewInternalError(err, "cannot update blueprint CR status %q", spec.Id) } diff --git a/pkg/adapter/maintenance/mode.go b/pkg/adapter/maintenance/mode.go index bf25335..0401c7c 100644 --- a/pkg/adapter/maintenance/mode.go +++ b/pkg/adapter/maintenance/mode.go @@ -24,25 +24,16 @@ func New(globalConfig registry.ConfigurationContext) *Mode { func (m *Mode) Activate(content domainservice.MaintenancePageModel) error { isActive, isOurs, err := m.lock.isActiveAndOurs() if err != nil { - return &domainservice.InternalError{ - WrappedError: err, - Message: "failed to check if maintenance mode is already active and ours", - } + return domainservice.NewInternalError(err, "failed to check if maintenance mode is already active and ours") } if isActive && !isOurs { - return &domainservice.ConflictError{ - WrappedError: nil, - Message: "cannot activate maintenance mode as it was already activated by another party", - } + return domainservice.NewConflictError(nil, "cannot activate maintenance mode as it was already activated by another party") } err = m.switcher.activate(content) if err != nil { - return &domainservice.InternalError{ - WrappedError: err, - Message: "failed to activate maintenance mode", - } + return domainservice.NewInternalError(err, "failed to activate maintenance mode") } return nil @@ -52,10 +43,7 @@ func (m *Mode) Activate(content domainservice.MaintenancePageModel) error { func (m *Mode) Deactivate() error { isActive, isOurs, err := m.lock.isActiveAndOurs() if err != nil { - return &domainservice.InternalError{ - WrappedError: err, - Message: "failed to check if maintenance mode is already active and ours", - } + return domainservice.NewInternalError(err, "failed to check if maintenance mode is already active and ours") } if !isActive { @@ -64,18 +52,12 @@ func (m *Mode) Deactivate() error { } if !isOurs { - return &domainservice.ConflictError{ - WrappedError: nil, - Message: "cannot deactivate maintenance mode as it was activated by another party", - } + return domainservice.NewConflictError(nil, "cannot deactivate maintenance mode as it was activated by another party") } err = m.switcher.deactivate() if err != nil { - return &domainservice.InternalError{ - WrappedError: err, - Message: "failed to deactivate maintenance mode", - } + return domainservice.NewInternalError(err, "failed to deactivate maintenance mode") } return nil diff --git a/pkg/domainservice/adapterInterfaces.go b/pkg/domainservice/adapterInterfaces.go index aad3187..0c1dfea 100644 --- a/pkg/domainservice/adapterInterfaces.go +++ b/pkg/domainservice/adapterInterfaces.go @@ -270,8 +270,8 @@ type ConflictError struct { // NewConflictError creates an ConflictError with a given message. The wrapped error may be nil. The error message must // omit the fmt.Errorf verb %w because this is done by ConflictError.Error(). -func NewConflictError(wrappedError error, message string, msgArgs ...any) *InternalError { - return &InternalError{WrappedError: wrappedError, Message: fmt.Sprintf(message, msgArgs...)} +func NewConflictError(wrappedError error, message string, msgArgs ...any) *ConflictError { + return &ConflictError{WrappedError: wrappedError, Message: fmt.Sprintf(message, msgArgs...)} } // Error marks the struct as an error. @@ -288,6 +288,6 @@ func (e *ConflictError) Unwrap() error { } func IsConflictError(err error) bool { - var internalError *InternalError - return errors.As(err, &internalError) + var conflictError *ConflictError + return errors.As(err, &conflictError) } diff --git a/pkg/domainservice/adapterInterfaces_test.go b/pkg/domainservice/adapterInterfaces_test.go index ace8784..218f053 100644 --- a/pkg/domainservice/adapterInterfaces_test.go +++ b/pkg/domainservice/adapterInterfaces_test.go @@ -118,3 +118,10 @@ func TestIsConflictError(t *testing.T) { assert.True(t, IsConflictError(fmt.Errorf("test: %w", NewConflictError(assert.AnError, "test")))) assert.False(t, IsConflictError(assert.AnError)) } + +func TestNewConflictError(t *testing.T) { + assert.Equal(t, + &ConflictError{assert.AnError, "message"}, + NewConflictError(assert.AnError, "message"), + ) +}