Skip to content

Commit

Permalink
Better name for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenlj committed Sep 23, 2024
1 parent 961200e commit 453476e
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test:

check:
go run honnef.co/go/tools/cmd/staticcheck ./...
go run golang.org/x/vuln/cmd/govulncheck -v ./...
go run golang.org/x/vuln/cmd/govulncheck ./...

mocks:
go run github.com/vektra/mockery/v2
Expand Down
6 changes: 3 additions & 3 deletions controllers/aiven_application/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ func (r *AivenApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Req
application.Status.SynchronizationState = rolloutFailed
cr := ctrl.Result{}

if errors.Is(err, utils.NotFoundError) {
if errors.Is(err, utils.ErrNotFound) {
cr.RequeueAfter = requeueInterval * 10
} else if !errors.Is(err, utils.UnrecoverableError) {
} else if !errors.Is(err, utils.ErrUnrecoverable) {
cr.RequeueAfter = requeueInterval
}

Expand All @@ -97,7 +97,7 @@ func (r *AivenApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Req
err := r.Get(ctx, req.NamespacedName, &application)
switch {
case k8serrors.IsNotFound(err):
return fail(fmt.Errorf("resource deleted from cluster; noop: %w", utils.UnrecoverableError))
return fail(fmt.Errorf("resource deleted from cluster; noop: %w", utils.ErrUnrecoverable))
case err != nil:
return fail(fmt.Errorf("unable to retrieve resource from cluster: %s", err))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (h KafkaHandler) Apply(ctx context.Context, application *aiven_nais_io_v1.A
})

if !strings.ContainsString(h.projects, projectName) {
err := fmt.Errorf("pool %s is not allowed in this cluster: %w", projectName, utils.UnrecoverableError)
err := fmt.Errorf("pool %s is not allowed in this cluster: %w", projectName, utils.ErrUnrecoverable)
utils.LocalFail("ValidatePool", application, err, logger)
return err
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (h KafkaHandler) provideServiceUser(ctx context.Context, application *aiven

suffix, err := createSuffix(application)
if err != nil {
err = fmt.Errorf("unable to create service user suffix: %s %w", err, utils.UnrecoverableError)
err = fmt.Errorf("unable to create service user suffix: %s %w", err, utils.ErrUnrecoverable)
utils.LocalFail("CreateSuffix", application, err, logger)
return nil, err
}
Expand All @@ -167,7 +167,7 @@ func (h KafkaHandler) provideServiceUser(ctx context.Context, application *aiven
} else {
serviceUserName, err = kafka_nais_io_v1.ServiceUserNameWithSuffix(application.Namespace, application.Name, suffix)
if err != nil {
err = fmt.Errorf("unable to create service user name: %s %w", err, utils.UnrecoverableError)
err = fmt.Errorf("unable to create service user name: %s %w", err, utils.ErrUnrecoverable)
utils.LocalFail("ServiceUserNameWithSuffix", application, err, logger)
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/kafka/kafka_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func (suite *KafkaHandlerTestSuite) TestInvalidPool() {
err := suite.kafkaHandler.Apply(suite.ctx, &application, secret, suite.logger)

suite.Error(err)
suite.True(errors.Is(err, utils.UnrecoverableError))
suite.True(errors.Is(err, utils.ErrUnrecoverable))
suite.NotNil(application.Status.GetConditionOfType(aiven_nais_io_v1.AivenApplicationLocalFailure))
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (s Handler) Apply(ctx context.Context, application *aiven_nais_io_v1.AivenA
hasErrors := len(errors) > 0

if hasErrors {
return fmt.Errorf("invalid secret name '%s': %w", secretName, utils.UnrecoverableError)
return fmt.Errorf("invalid secret name '%s': %w", secretName, utils.ErrUnrecoverable)
}

updateObjectMeta(application, &secret.ObjectMeta)
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/secret/secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ var _ = Describe("secret.Handler", func() {
Build()
err := handler.Apply(ctx, &application, &corev1.Secret{}, nil)
Expect(err).ToNot(Succeed())
Expect(errors.Is(err, utils.UnrecoverableError)).To(BeTrue())
Expect(errors.Is(err, utils.ErrUnrecoverable)).To(BeTrue())
},
EntryDescription("%v"),
Entry("<empty>", ""),
Expand Down
8 changes: 4 additions & 4 deletions pkg/utils/error_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"k8s.io/apimachinery/pkg/util/json"
)

var UnrecoverableError = errors.New("UnrecoverableError")
var NotFoundError = errors.New("NotFoundError")
var ErrUnrecoverable = errors.New("ErrUnrecoverable")
var ErrNotFound = errors.New("ErrNotFound")

func AivenFail(operation string, application *aiven_nais_io_v1.AivenApplication, err error, notFoundIsRecoverable bool, logger logrus.FieldLogger) error {
errorMessage := UnwrapAivenError(err, logger, notFoundIsRecoverable)
Expand Down Expand Up @@ -42,10 +42,10 @@ func UnwrapAivenError(errorMessage error, logger logrus.FieldLogger, notFoundIsR
message = apiMessage.Message
}
if aivenErr.Status == 404 && notFoundIsRecoverable {
return fmt.Errorf("%s: %w", message, NotFoundError)
return fmt.Errorf("%s: %w", message, ErrNotFound)
}
if 400 <= aivenErr.Status && aivenErr.Status < 500 {
return fmt.Errorf("%s: %w", message, UnrecoverableError)
return fmt.Errorf("%s: %w", message, ErrUnrecoverable)
} else {
return fmt.Errorf("%s", message)
}
Expand Down

0 comments on commit 453476e

Please sign in to comment.