ADR 0052: `f().wrap("context")` wraps a call's error when it fails
Status: Accepted (2026-10-27). Extends the prelude’s err.wrap (errors.md
§4.3). Breaking only for .wrap on a call that returns nothing but an error,
used as a value.
Context
Section titled “Context”Adding context to an error took three lines:
data := os.ReadFile(path) catch (err) { throw err.wrap("reading config ${path}")}A bare ? takes one, so it wins: the repository’s examples propagate with a
bare ? 48 times, while the examples and benchmarks together wrap 3 times,
and production errors end up without context. Rust settled the same tension with .context("…")?.
Writing .wrap on the call was already possible in one case, with the wrong
meaning: on a call that returns only an error, validate().wrap("ctx") was
the error value wrapped as is, so a nil error (success) became a non-nil
"ctx: %!w(<nil>)".
Decision
Section titled “Decision”-
f().wrap(msg)on a call that can fail is that call with its error wrapped when it fails, and is still a call that can fail:data := os.ReadFile(path).wrap("reading config ${path}")?lowers to the Go of the
catchform above:data, err := os.ReadFile(path)if err != nil {return "", fmt.Errorf("reading config %s: %w", path, err)}The message is evaluated only on failure. A call that returns only an error is the same: when it returns nil, nothing is wrapped.
-
Handled where it is written. The error is wrapped in the branch that handles it, so the call is the operand of
?or of an untypedcatch, whose binder is then the wrapped error. Elsewhere (a typedcatch, a pass-throughreturn, destructuring, a value) it is E0216, with hints for?,catch, and for wrapping an error value (e := f(), thene.wrap(...)). -
err.wrap(msg)on an error value is unchanged.
Alternatives
Section titled “Alternatives”- A new operator or keyword (
f() ? "context",f() catch wrap "…"): more syntax for what a method reads as plainly. - Allow it everywhere (destructuring, typed
catch, pass-through): each form would need its own wrapping branch for little use; the handler of a typedcatchcan calle.wrap(...). - Status quo: errors keep arriving without context.
Consequences
Section titled “Consequences”- Context costs a few characters per call, in the Go a careful programmer writes.
f().wrap(msg)used as a value on a call that returns only an error is now E0216; the guide’s version table has a row. Nothing in the repository used it.
Implementation
Section titled “Implementation”- Checker.
selectorininternal/check/member.gotypes.wrapon a call that can fail as that call (PreludeWrapFailure);callreports it outside?and an untypedcatch(wrapHandled). - Lowering. The call lowers to itself;
tryIntowraps the error in the failure branch andcatchHandlerassigns the wrapped error to the binder (wrapOnFailure,wrapErrininternal/lower). - Tests.
run/wrapcall(propagation, catch, a message evaluated only on failure, an error-only call that succeeds,errors.Isthrough the wrap),gen/wrapcall(the generated Go),check/wrapcall_neg(the places it is rejected).