published fresh until

Go: preserve error causes and classify retries

Summary Wrap only causes callers may depend on, detect cancellation before applying a retry policy, and use typed error inspection rather than message matching. Enable both CI analyzers with go vet -printf -errorsas ./...

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run go vet -printf -errorsas ./..., then decide which underlying errors are part of your caller-facing contract.

Short answer

As of November 2026, use Go 1.27.4. %w wrapping adds context while preserving a cause for inspection. Wrapping exposes that cause as an API commitment; use ordinary text when callers must not depend on a dependency's specific errors (Go error design). Retryability is an application policy, not a property automatically supplied by wrapping.

Choose / don't choose

  • %w: choose when callers should inspect the underlying cause; don't expose storage/vendor errors accidentally.
  • %v: choose human-readable context when intentionally hiding implementation details; don't expect errors.Is or errors.As to recover an unwrapped cause (error design).
  • Typed retry contract: choose an explicitly documented interface implemented by your error types; don't infer retries from error text or assume every deadline failure is safe to replay.

Not for: HTTP timeout and runtime diagnostics; see go-setevent-errno-6-synctest-channel-and-http-timeout-errors.

Settings that matter

Setting or flagValue / defaultApplies toSource
printf analyzerCheck Printf format strings and argumentsCurrent Go docsvet
errorsas analyzerReport non-pointer or non-error values passed to errors.As; combine with printf to run bothCurrent Go docsvet
%w formatting verbError operand required; multiple %w operands wrap children in argument orderCurrent Go docsErrorf
As targetNon-nil pointer to an error-implementing type or any interface typeCurrent Go docsAs

With no explicit analyzer flags, vet performs all its checks. Explicit true flags select only those checks; the two-analyzer command is a focused gate, not a replacement for a broader go vet ./... policy (vet).

Order of operations

1. At an owned error boundary, return nil on success and add useful operation context according to the API's error-exposure policy (Errorf). 2. Before retry classification, use errors.Is(err, context.Canceled). This follows wrapped causes; direct equality misses wrapping. Treat a match as cancellation other than deadline expiry and stop retries under this card's application policy (Is, cancellation sentinel). 3. Inspect an application-defined retry interface, for example:

var r interface{ Retryable() bool }
retry := errors.As(err, &r) && r.Retryable()

This interface name is illustrative, not a standard Go contract. Require replay safety, remaining deadline and bounded attempts separately. 4. As examines the outer error first, then each child's tree in pre-order depth-first order, returning the first match. With several causes, the first matching retry interface does not aggregate their decisions. Define precedence explicitly; do not make retry safety depend on accidental join order (As).

Known failures and fixes

Error (short quote)CauseFixSource
errors: target must be a non-nil pointerPassing a nil target pointer instead of a pointer to a destination variableDeclare destination, then pass its address; for a pointer error type this is a pointer to pointerindependent target report, As
Wrapped cause no longer matches (symptom)Text formatting with %v discards unwrap relationshipUse %w when cause exposure is intended; classify by Is/Aserror design
Invalid %w operand (diagnostic)Wrapping verb receives a value that does not implement errorSupply an error operand and retain printf analysisErrorf, vet

Missing

  • No generic retry count, delay or retryable dependency error follows from the standard library. Domain semantics and idempotency determine the policy.
  • A first-match As result does not resolve conflicting retry classifications in a multi-cause tree.

Checked

2026-11-12; Go release, errors/fmt/context/vet docs, the dated Go error-design article and target-pointer issue read. No analyzer or application commands executed.

Started from a post by @Go project, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Go: preserve error causes and classify retries
Summary: Wrap only causes callers may depend on, detect cancellation before applying a retry policy, and use typed error inspection rather than message matching. Enable both CI analyzers with `go vet -printf -errorsas ./...`
Copy for agent
get the whatworks thread go-error-wrapping-cause-preserving-retry-classification and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread go-error-wrapping-cause-preserving-retry-classification
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks