From 453476ea0d0f62142a718f9354210ab271357b2f Mon Sep 17 00:00:00 2001 From: Morten Lied Johansen Date: Mon, 23 Sep 2024 11:06:00 +0200 Subject: [PATCH] Better name for errors --- Makefile | 2 +- controllers/aiven_application/reconciler.go | 6 +++--- pkg/handlers/kafka/kafka.go | 6 +++--- pkg/handlers/kafka/kafka_test.go | 2 +- pkg/handlers/secret/secret.go | 2 +- pkg/handlers/secret/secret_test.go | 2 +- pkg/utils/error_handling.go | 8 ++++---- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 28c6d14..e373ced 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/controllers/aiven_application/reconciler.go b/controllers/aiven_application/reconciler.go index f17470b..77b93b0 100644 --- a/controllers/aiven_application/reconciler.go +++ b/controllers/aiven_application/reconciler.go @@ -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 } @@ -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)) } diff --git a/pkg/handlers/kafka/kafka.go b/pkg/handlers/kafka/kafka.go index 63a3d2b..95b2609 100644 --- a/pkg/handlers/kafka/kafka.go +++ b/pkg/handlers/kafka/kafka.go @@ -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 } @@ -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 } @@ -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 } diff --git a/pkg/handlers/kafka/kafka_test.go b/pkg/handlers/kafka/kafka_test.go index fc457fa..fd50733 100644 --- a/pkg/handlers/kafka/kafka_test.go +++ b/pkg/handlers/kafka/kafka_test.go @@ -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)) } diff --git a/pkg/handlers/secret/secret.go b/pkg/handlers/secret/secret.go index 6fc9e15..96fc8d8 100644 --- a/pkg/handlers/secret/secret.go +++ b/pkg/handlers/secret/secret.go @@ -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) diff --git a/pkg/handlers/secret/secret_test.go b/pkg/handlers/secret/secret_test.go index 1acd9cf..cb76195 100644 --- a/pkg/handlers/secret/secret_test.go +++ b/pkg/handlers/secret/secret_test.go @@ -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("", ""), diff --git a/pkg/utils/error_handling.go b/pkg/utils/error_handling.go index 7380c08..cc7bbd7 100644 --- a/pkg/utils/error_handling.go +++ b/pkg/utils/error_handling.go @@ -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) @@ -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) }