From f5d691c5734026400ed990b5f2861267efff4536 Mon Sep 17 00:00:00 2001 From: "Per G. da Silva" Date: Wed, 2 Dec 2026 10:53:13 +0100 Subject: [PATCH] Use errors.AsType instead of errors.As Go 1.26 added the generic errors.AsType, which returns the matched error and a bool. Using it removes the pre-declared target variables, scopes the matched error to the if statement, and checks the target type at compile time instead of at run time. This is a mechanical change with no behavior change. It covers every errors.As call site in the repository. Signed-off-by: Per G. da Silva --- .../controllers/clusterobjectset_controller.go | 9 +++------ internal/operator-controller/config/config.go | 6 ++---- internal/shared/util/error/terminal.go | 3 +-- internal/shared/util/http/certlog.go | 3 +-- test/internal/e2e/platform/steps/demo_steps.go | 3 +-- test/internal/e2e/steps/hooks.go | 3 +-- test/internal/e2e/steps/steps.go | 6 ++---- 7 files changed, 11 insertions(+), 22 deletions(-) diff --git a/internal/object-controller/controllers/clusterobjectset_controller.go b/internal/object-controller/controllers/clusterobjectset_controller.go index 784675b2e5..ff8f77446d 100644 --- a/internal/object-controller/controllers/clusterobjectset_controller.go +++ b/internal/object-controller/controllers/clusterobjectset_controller.go @@ -150,8 +150,7 @@ func (c *ClusterObjectSetReconciler) reconcile(ctx context.Context, cos *ocv1.Cl } if err := c.verifyReferencedSecretsImmutable(ctx, cos); err != nil { - var mutErr *mutableSecretError - if errors.As(err, &mutErr) { + if _, ok := errors.AsType[*mutableSecretError](err); ok { // A confirmed mutable Secret requires user action (replacing the immutable Secret), // which is not watched, so keep retrying so the revision can recover. l.Error(err, "referenced Secret is mutable, marking revision not ready") @@ -165,8 +164,7 @@ func (c *ClusterObjectSetReconciler) reconcile(ctx context.Context, cos *ocv1.Cl phases, currentPhases, opts, err := c.buildBoxcutterPhases(ctx, cos) if err != nil { - var contentErr *referencedContentError - if errors.As(err, &contentErr) { + if _, ok := errors.AsType[*referencedContentError](err); ok { // Invalid referenced Secret content (missing key or malformed manifest). The Secret // is not watched and the ref is immutable, so keep retrying so the revision can // recover once the Secret is replaced with valid content. @@ -174,8 +172,7 @@ func (c *ClusterObjectSetReconciler) reconcile(ctx context.Context, cos *ocv1.Cl markAsNotReady(cos, ocv1.ClusterObjectSetReasonPreconditionsNotMet, fmt.Sprintf("Referenced content is invalid: %v. Fix the Secret content.", err)) return ctrl.Result{RequeueAfter: validationRetryInterval}, nil } - var cfgErr *configValidationError - if errors.As(err, &cfgErr) { + if _, ok := errors.AsType[*configValidationError](err); ok { // Invalid revision spec; a spec edit (or a new revision) re-triggers reconciliation, // so no periodic retry is needed. l.Error(err, "invalid configuration, marking revision not ready") diff --git a/internal/operator-controller/config/config.go b/internal/operator-controller/config/config.go index a814b50258..1413956d06 100644 --- a/internal/operator-controller/config/config.go +++ b/internal/operator-controller/config/config.go @@ -245,8 +245,7 @@ func validateConfigWithSchema(configBytes []byte, schema map[string]any, install // formatSchemaError converts JSON schema validation errors into user-friendly messages. // If multiple validation errors exist, it combines them into a single error message. func formatSchemaError(err error) error { - ve := &jsonschema.ValidationError{} - ok := errors.As(err, &ve) + ve, ok := errors.AsType[*jsonschema.ValidationError](err) if !ok { // Not a ValidationError, return as-is // Caller (UnmarshalConfig) will add "invalid configuration:" prefix @@ -401,8 +400,7 @@ func buildFieldPath(location string) string { // formatUnmarshalError makes YAML/JSON parsing errors easier to understand. func formatUnmarshalError(err error) error { - var typeErr *json.UnmarshalTypeError - if errors.As(err, &typeErr) { + if typeErr, ok := errors.AsType[*json.UnmarshalTypeError](err); ok { if typeErr.Field == "" { return errors.New("input is not a valid JSON object") } diff --git a/internal/shared/util/error/terminal.go b/internal/shared/util/error/terminal.go index ee4bbd0f1c..ed0825b7f2 100644 --- a/internal/shared/util/error/terminal.go +++ b/internal/shared/util/error/terminal.go @@ -50,8 +50,7 @@ func ExtractTerminalReason(err error) (string, bool) { } // Unwrap the reconcile.TerminalError wrapper first unwrapped := errors.Unwrap(err) - var terr *terminalErrorWithReason - if errors.As(unwrapped, &terr) { + if terr, ok := errors.AsType[*terminalErrorWithReason](unwrapped); ok { return terr.reason, true } return "", false diff --git a/internal/shared/util/http/certlog.go b/internal/shared/util/http/certlog.go index 60823140af..4e275b99bd 100644 --- a/internal/shared/util/http/certlog.go +++ b/internal/shared/util/http/certlog.go @@ -65,8 +65,7 @@ func LogDockerCertificates(path string, log logr.Logger) { // * false if no CertificateVerificationError is found func LogCertificateVerificationError(err error, log logr.Logger) bool { for err != nil { - var cvErr *tls.CertificateVerificationError - if errors.As(err, &cvErr) { + if cvErr, ok := errors.AsType[*tls.CertificateVerificationError](err); ok { n := 1 for _, cert := range cvErr.UnverifiedCertificates { log.Error(err, "unverified cert", "n", n, "subject", cert.Subject, "issuer", cert.Issuer, "DNSNames", cert.DNSNames, "serial", cert.SerialNumber) diff --git a/test/internal/e2e/platform/steps/demo_steps.go b/test/internal/e2e/platform/steps/demo_steps.go index 5bea94a102..903f1f7a16 100644 --- a/test/internal/e2e/platform/steps/demo_steps.go +++ b/test/internal/e2e/platform/steps/demo_steps.go @@ -47,8 +47,7 @@ func bash(ctx context.Context, script string) (string, error) { if err != nil { shared.Log().V(1).Info("Failed to run", "command", script, "stderr", stderr, "error", err) - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { exitErr.Stderr = stderrBuf.Bytes() } } diff --git a/test/internal/e2e/steps/hooks.go b/test/internal/e2e/steps/hooks.go index 5005259825..f00e4f27da 100644 --- a/test/internal/e2e/steps/hooks.go +++ b/test/internal/e2e/steps/hooks.go @@ -256,8 +256,7 @@ func Scenario(ctx context.Context) *World { } func stderrOutput(err error) string { - var exitErr *exec.ExitError - if errors.As(err, &exitErr) && exitErr != nil { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok && exitErr != nil { return string(exitErr.Stderr) } return "" diff --git a/test/internal/e2e/steps/steps.go b/test/internal/e2e/steps/steps.go index 10b0ada512..5a7a9005d4 100644 --- a/test/internal/e2e/steps/steps.go +++ b/test/internal/e2e/steps/steps.go @@ -240,8 +240,7 @@ func K8sClient(ctx context.Context, args ...string) (string, error) { logger.V(1).Info("Failed to run", "command", strings.Join(cmd.Args, " "), "stderr", stderrStr, "error", err) // Inject stderr into ExitError so stderrOutput() can extract it. // cmd.Run() with explicit cmd.Stderr does not populate ExitError.Stderr. - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { exitErr.Stderr = []byte(stderrStr) } } @@ -270,8 +269,7 @@ func K8sCLIWithInput(ctx context.Context, yaml string, args ...string) (string, stderrStr := stderrBuf.String() if err != nil { - var exitErr *exec.ExitError - if errors.As(err, &exitErr) { + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { exitErr.Stderr = []byte(stderrStr) } }