diff --git a/pkg/adapter/config/kubernetes/errorhandling.go b/pkg/adapter/config/kubernetes/errorhandling.go new file mode 100644 index 0000000..735ed6b --- /dev/null +++ b/pkg/adapter/config/kubernetes/errorhandling.go @@ -0,0 +1,24 @@ +package kubernetes + +import ( + "github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice" + liberrors "github.com/cloudogu/k8s-registry-lib/errors" +) + +func mapToBlueprintError(err error) error { + if err != nil { + 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") + } else if liberrors.IsConnectionError(err) { + return domainservice.NewInternalError(err, "could not load/update config due to connection problems") + } else if liberrors.IsAlreadyExistsError(err) { + return domainservice.NewInternalError(err, "could not create config as it already exists") + } else { + // GenericError and fallback if even that would not match the error + return domainservice.NewInternalError(err, "could not load/update config due to an unknown problem") + } + } + return nil +} diff --git a/pkg/adapter/config/kubernetes/globalConfigRepository.go b/pkg/adapter/config/kubernetes/globalConfigRepository.go index d6f10cb..1bf1a8a 100644 --- a/pkg/adapter/config/kubernetes/globalConfigRepository.go +++ b/pkg/adapter/config/kubernetes/globalConfigRepository.go @@ -2,9 +2,8 @@ package kubernetes import ( "context" - "github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice" + "fmt" "github.com/cloudogu/k8s-registry-lib/config" - liberrors "github.com/cloudogu/k8s-registry-lib/errors" ) type GlobalConfigRepository struct { @@ -18,14 +17,7 @@ func NewGlobalConfigRepository(repo k8sGlobalConfigRepo) *GlobalConfigRepository func (e GlobalConfigRepository) Get(ctx context.Context) (config.GlobalConfig, error) { loadedConfig, err := e.repo.Get(ctx) if err != nil { - if liberrors.IsNotFoundError(err) { - return loadedConfig, domainservice.NewNotFoundError(err, "could not find global config. Check if your ecosystem is ready for operation") - } else if liberrors.IsConnectionError(err) { - return loadedConfig, domainservice.NewInternalError(err, "could not load global config due to connection problems") - } else { - // GenericError and fallback if even that would not match the error - return loadedConfig, domainservice.NewInternalError(err, "could not load global config due to an unknown problem") - } + return loadedConfig, fmt.Errorf("could not load global config: %w", mapToBlueprintError(err)) } return loadedConfig, nil } @@ -33,16 +25,7 @@ func (e GlobalConfigRepository) Get(ctx context.Context) (config.GlobalConfig, e func (e GlobalConfigRepository) Update(ctx context.Context, config config.GlobalConfig) (config.GlobalConfig, error) { updatedConfig, err := e.repo.Update(ctx, config) if err != nil { - if liberrors.IsNotFoundError(err) { - return updatedConfig, domainservice.NewNotFoundError(err, "could not update global config. Check if your ecosystem is ready for operation") - } else if liberrors.IsConnectionError(err) { - return updatedConfig, domainservice.NewInternalError(err, "could not update global config due to connection problems") - } else if liberrors.IsConflictError(err) { - return updatedConfig, domainservice.NewInternalError(err, "could not update global config due to conflicting changes") - } else { - // GenericError and fallback if even that would not match the error - return updatedConfig, domainservice.NewInternalError(err, "could not update global config due to an unknown problem") - } + return updatedConfig, fmt.Errorf("could not update global config: %w", mapToBlueprintError(err)) } return updatedConfig, nil } diff --git a/pkg/adapter/config/kubernetes/globalConfigRepository_test.go b/pkg/adapter/config/kubernetes/globalConfigRepository_test.go index 618f0f0..7242942 100644 --- a/pkg/adapter/config/kubernetes/globalConfigRepository_test.go +++ b/pkg/adapter/config/kubernetes/globalConfigRepository_test.go @@ -62,7 +62,7 @@ func TestGlobalConfigRepository_Get(t *testing.T) { _, err := repo.Get(testCtx) //then assert.ErrorContains(t, err, givenError.Error()) - assert.ErrorContains(t, err, "could not load global config due to connection problems") + assert.ErrorContains(t, err, "could not load global config") assert.True(t, domainservice.IsInternalError(err), "error is no InternalError") }) t.Run("internal error on all other errors", func(t *testing.T) { @@ -76,7 +76,7 @@ func TestGlobalConfigRepository_Get(t *testing.T) { _, err := repo.Get(testCtx) //then assert.ErrorContains(t, err, givenError.Error()) - assert.ErrorContains(t, err, "could not load global config due to an unknown problem") + assert.ErrorContains(t, err, "could not load global config") assert.True(t, domainservice.IsInternalError(err), "error is no InternalError") }) } @@ -117,7 +117,7 @@ func TestGlobalConfigRepository_Update(t *testing.T) { _, err := repo.Update(testCtx, testGlobalConfig) //then assert.ErrorContains(t, err, givenError.Error()) - assert.ErrorContains(t, err, "could not update global config due to conflicting changes") + assert.ErrorContains(t, err, "could not update global config") assert.True(t, domainservice.IsConflictError(err), "error is no ConflictError") }) t.Run("internal error if connection error happens", func(t *testing.T) { @@ -131,7 +131,7 @@ func TestGlobalConfigRepository_Update(t *testing.T) { _, err := repo.Update(testCtx, testGlobalConfig) //then assert.ErrorContains(t, err, givenError.Error()) - assert.ErrorContains(t, err, "could not update global config due to connection problems") + assert.ErrorContains(t, err, "could not update global config") assert.True(t, domainservice.IsInternalError(err), "error is no InternalError") }) t.Run("internal error on all other errors", func(t *testing.T) { @@ -145,7 +145,7 @@ func TestGlobalConfigRepository_Update(t *testing.T) { _, err := repo.Update(testCtx, testGlobalConfig) //then assert.ErrorContains(t, err, givenError.Error()) - assert.ErrorContains(t, err, "could not update global config due to an unknown problem") + assert.ErrorContains(t, err, "could not update global config") assert.True(t, domainservice.IsInternalError(err), "error is no InternalError") }) }