Skip to content

Commit

Permalink
#81 fix bug while mapping conflict errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Oct 15, 2024
1 parent e0a25e8 commit bfcbd7e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pkg/adapter/config/kubernetes/errorhandling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 2 additions & 8 deletions pkg/adapter/kubernetes/blueprintcr/blueprintSpecCRRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
30 changes: 6 additions & 24 deletions pkg/adapter/maintenance/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pkg/domainservice/adapterInterfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
7 changes: 7 additions & 0 deletions pkg/domainservice/adapterInterfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)
}

0 comments on commit bfcbd7e

Please sign in to comment.