Go error wrapping is an API contract, not just a style choice
A developer revisiting Go error-handling practices discovered that conventional wisdom on wrapping errors is more nuanced than commonly taught. Dave Cheney, who popularized wrapping errors at every layer via pkg/errors in 2016, later reversed course in 2021, arguing that structured logging makes nested error wraps redundant. The real distinction, as clarified by Go 1.13's %w verb in fmt.Errorf, is that wrapping an error exposes it as an inspectable API contract, allowing callers to use errors.Is and errors.As to match on specific error types. This means the decision to wrap is not merely about producing readable error messages, but about deliberately choosing what internal errors to surface to callers. Whether to wrap at every layer, once at a package boundary, or not at all depends on whether the goal is a clean production message or a stable, queryable error interface.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in