Skip to content

Commit

Permalink
#81 extract error mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Oct 15, 2024
1 parent c8d5d4e commit ef2f5ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
24 changes: 24 additions & 0 deletions pkg/adapter/config/kubernetes/errorhandling.go
Original file line number Diff line number Diff line change
@@ -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
}
23 changes: 3 additions & 20 deletions pkg/adapter/config/kubernetes/globalConfigRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -18,31 +17,15 @@ 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
}

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
}
10 changes: 5 additions & 5 deletions pkg/adapter/config/kubernetes/globalConfigRepository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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")
})
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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")
})
}

0 comments on commit ef2f5ec

Please sign in to comment.