Go / Intermediate: Packages and Concurrent Work / Lesson 14
Error wrapping and classification
Wrap and classify Go errors while preserving deliberate API contracts.
On this page
Lesson goals
- Wrap an error and match its cause.
- Choose which error details form a public contract.
- Classify wrapped error trees under deliberate public contracts.
- Use Is, As and Join with explicit identity, type and partial-result boundaries.
Define the error contract
An error value communicates failure across an API boundary. A caller may need both context about the operation and a stable way to classify its cause. Avoid parsing human-readable error strings for control flow: messages can change without preserving identity. Decide which causes are part of the public contract before exposing them through wrapping.
Wrap and match causes
fmt.Errorf with %w wraps an error while adding context. errors.Is can then inspect the error chain for a target error. The example uses a synthetic sentinel and verifies its identity after wrapping. The added message helps a person locate the operation; the identity check supports program logic without depending on the exact text. Return nil only when the operation actually succeeded.
package main
import (
"errors"
"fmt"
)
func main() {
missing := errors.New("missing")
err := fmt.Errorf("load record: %w", missing)
fmt.Println(errors.Is(err, missing))
}
true
errors.Is finds the sentinel through the wrapper.
Extract an error type
errors.As locates an error in the chain that can be assigned to a requested type. The example defines a small error type and extracts its code after wrapping. Treat the extracted fields as part of an intentional contract. If callers only need a broad classification, avoid exposing internal implementation types that would make future changes unnecessarily difficult.
package main
import (
"errors"
"fmt"
)
type problem struct { code int }
func (p *problem) Error() string { return "problem" }
func main() {
err := fmt.Errorf("read: %w", &problem{code: 7})
var p *problem
if errors.As(err, &p) {
fmt.Println(p.code)
}
}
7
errors.As extracts the wrapped problem value.
Choose what to expose
Wrapping makes the underlying error inspectable and can therefore become a compatibility promise. Add context at a layer that knows the operation, and choose whether the underlying cause should be exposed. A message using %v describes an error but does not wrap it in the same way. Do not include secrets or untrusted payloads merely to make a message more detailed.
Test error behavior
Test a wrapped matching error, a different error, and a successful result. Check classification with errors.Is or errors.As rather than asserting every character of a message unless that text is itself a documented interface. Include a consumer test for an exported error contract and review whether a later implementation change would accidentally break callers.
Separate human context from machine classification
An error is a value returned across an API boundary, and its contract should say what callers may rely on. A message can identify the failed operation for a person, but exact wording is usually a poor control-flow protocol: two independently created errors can have the same text without being the same sentinel, and a message can change while the category remains stable. When callers need decisions, document a package-owned sentinel, an error type or another explicit classification. State whether the accompanying result is unusable or may contain a partial value on failure.
Check a non-nil error before using a result whose validity depends on success. On a genuine success return a nil error interface, not a nil pointer of a type implementing error converted into that interface. The latter carries a dynamic type and can compare non-nil. Test success, a documented matching failure and an unrelated failure through the declared public API. Avoid exposing secrets, raw request bodies or private infrastructure details in either error messages or logs merely to add context.
Follow deliberate wrapping relationships
fmt.Errorf with %w adds context and creates a wrapping relationship to the supplied error. errors.Is inspects the error tree, including wrappers, for a match with a target; direct equality with the outer wrapper normally does not find an inner sentinel. Matching is not solely string comparison: errors.Is can also use a documented custom Is method. Use a stable target supplied by the API and test both a matching wrapped failure and a different failure, rather than reconstructing a new errors.New value with matching text.
Formatting with %v can include a cause’s text without exposing it through the same unwrap relationship. This does not automatically make the text safe to disclose: redact or translate private detail before it crosses a public boundary. Wrapping an implementation-specific database error with %w allows clients to depend on its identity; changing the backend can then affect them. Translate to a package-owned category when that is the intended compatibility boundary, and preserve internal diagnostics separately under the application’s access policy.
Extract a documented type safely
errors.As searches the error tree for the first error assignable to the requested target type, or for a match supplied by an appropriate custom As method. Declare a variable of the target type and pass its address; for an error implemented by *LimitError the common shape is var target *LimitError followed by errors.As(err, &target). Read fields only when As returns true. A direct type assertion against the outer wrapper checks that outer dynamic value and may miss a nested error.
The target argument must meet the errors.As requirements; passing an invalid target panics rather than reporting a normal mismatch. Exposing a concrete error type and its fields lets callers depend on those details, so document stability, mutability and ownership deliberately. Use a broad public category when callers do not need backend-specific fields. Test a direct instance, a wrapped instance, an unrelated error and nil, using fresh target state for each independent assertion.
Understand joined errors and traversal
Go 1.22 includes errors.Join, which can combine multiple non-nil errors into a value that unwraps to a slice of errors. errors.Is and errors.As traverse the resulting tree in the documented pre-order, depth-first order. A matching category can therefore be found in a child even when another child is present; errors.As returns the first matching value in traversal order, so callers should not assume a different matching instance is selected. errors.Join with only nil inputs returns nil. Do not put nil elements into the slice returned by a custom Unwrap() []error implementation.
A joined error can be useful when an operation and its cleanup both fail, but the API must state which categories callers may inspect and how it reports partial effects. Combining failures does not undo an already completed write or release callers from checking state. For a custom wrapper, implement and test the documented Unwrap contract without creating cycles or changing error identity accidentally. In concurrent code, publish and inspect shared error state only through an appropriate synchronization protocol; wrapping itself supplies no synchronization for surrounding mutable data.
Test the public boundary and its limits
Build tests around the intended public decisions: a wrapped matching sentinel, a nonmatching sentinel with the same or different text, a documented type found through a wrapper, and a nil success. If a package translates private errors, verify the public category matches while the private identity is not exposed through the returned tree. Avoid asserting every character of a message unless wording itself is a documented interface; do assert that synthetic secret markers do not appear in public output.
Use controlled fakes for failure and cleanup so the test can inspect category, partial result and state without contacting production infrastructure. A unit fake proves only the behavior it models, not a remote service’s actual failures. For concurrency, coordinate workers and publish results through channels, locks or a join; a race detector can report races in exercised executions but cannot prove every schedule safe. The following traces are synthetic and unexecuted, and do not claim Go runtime or integration verification.
20 worked examples
1. Direct sentinel
Problem: Compare a returned ErrMissing sentinel with the same documented target.
- The error value is the same documented sentinel.
- errors.Is checks the error itself before unwrapping.
err=ErrMissing; errors.Is(err,ErrMissing)
The fixture specifies values and relationships without claiming program execution.
Expected result: The match is true.
Why this result follows: The target identity is directly present.
2. Wrapped sentinel
Problem: Wrap ErrMissing with operation context using %w.
- The outer error adds load context.
- The %w wrapper exposes the inner sentinel to traversal.
err=fmt.Errorf("load: %w",ErrMissing); errors.Is(err,ErrMissing)
The fixture specifies values and relationships without claiming program execution.
Expected result: errors.Is returns true.
Why this result follows: The wrapping relationship preserves the intended identity.
3. Direct equality
Problem: Compare an outer wrapper directly with its wrapped sentinel.
- The formatted wrapper is a distinct outer error value.
- Direct equality does not traverse the wrapper.
err=fmt.Errorf("load: %w",ErrMissing); err==ErrMissing
The fixture specifies values and relationships without claiming program execution.
Expected result: The direct comparison is false in this fixture.
Why this result follows: Use errors.Is to inspect an exposed nested sentinel.
4. Same message
Problem: Create two independent errors with message missing and compare them.
- Each errors.New call creates a distinct error value.
- Matching message text does not establish sentinel identity.
a=errors.New("missing"); b=errors.New("missing"); errors.Is(a,b)
The fixture specifies values and relationships without claiming program execution.
Expected result: errors.Is(a,b) is false.
Why this result follows: Classification must not infer identity from identical wording.
5. Text only
Problem: Format ErrMissing using %v and test errors.Is.
- The resulting message includes the cause text.
- The %v verb does not create a wrapping relationship.
err=fmt.Errorf("load: %v",ErrMissing); errors.Is(err,ErrMissing)
The fixture specifies values and relationships without claiming program execution.
Expected result: The sentinel match is false for this formatted error.
Why this result follows: Textual inclusion alone does not expose the error identity.
6. Multiple wraps
Problem: Wrap ErrMissing twice with %w and inspect it.
- The outer wrapper exposes the inner wrapper.
- The inner wrapper exposes ErrMissing.
inner=fmt.Errorf("read: %w",ErrMissing); outer=fmt.Errorf("load: %w",inner); errors.Is(outer,ErrMissing)
The fixture specifies values and relationships without claiming program execution.
Expected result: errors.Is returns true through both levels.
Why this result follows: Traversal follows the deliberate unwrap relationships.
7. Unrelated sentinel
Problem: Inspect a wrapped ErrDenied for the unrelated ErrMissing target.
- The wrapper contains the denied sentinel.
- The fixture supplies no match for the distinct missing sentinel.
err=fmt.Errorf("read: %w",ErrDenied); errors.Is(err,ErrMissing)
The fixture specifies values and relationships without claiming program execution.
Expected result: errors.Is returns false.
Why this result follows: An exposed error chain does not match an unrelated target.
8. Custom Is
Problem: A documented error type implements Is to match ErrTemporary.
- The custom Is method reports a match for the target.
- errors.Is consults the method as part of matching.
err=customError with Is(ErrTemporary)=true; errors.Is(err,ErrTemporary)
The fixture specifies values and relationships without claiming program execution.
Expected result: The result is true under the stated implementation.
Why this result follows: A documented custom Is method can define category matching.
9. As direct type
Problem: Inspect a direct *LimitError with Limit=5 using errors.As.
- The error is assignable to the target error type.
- As stores the matched pointer in target.
err=&LimitError{Limit:5}; var target *LimitError; errors.As(err,&target)
The fixture specifies values and relationships without claiming program execution.
Expected result: The match is true and target.Limit is 5.
Why this result follows: The target address permits As to return the matching value.
10. As wrapped type
Problem: Inspect a wrapped *LimitError with Limit=8.
- The outer error is a wrapper, not the target type itself.
- As traverses to the nested matching error.
err=fmt.Errorf("send: %w",&LimitError{Limit:8}); var target *LimitError; errors.As(err,&target)
The fixture specifies values and relationships without claiming program execution.
Expected result: The match is true and target.Limit is 8.
Why this result follows: errors.As can extract a documented type through wrapping.
11. As mismatch
Problem: Inspect an unrelated error for *LimitError with a fresh target.
- The error tree contains no matching LimitError in this fixture.
- The caller checks ok before using target.
err=ErrMissing; var target *LimitError; ok=errors.As(err,&target)
The fixture specifies values and relationships without claiming program execution.
Expected result: ok is false and the caller takes the nonmatching branch.
Why this result follows: A failed As lookup must not be treated as a populated result.
12. Direct assertion
Problem: Assert a wrapper directly as *LimitError while its nested cause has that type.
- The dynamic value at the outer level is the wrapper.
- A direct assertion does not traverse its nested cause.
outer=fmt.Errorf("read: %w",&LimitError{Limit:5}); _,ok=outer.(*LimitError)
The fixture specifies values and relationships without claiming program execution.
Expected result: The direct assertion fails in this fixture.
Why this result follows: Use errors.As when the public contract permits nested type inspection.
13. Join two errors
Problem: Join ErrWrite and ErrClose after an operation and cleanup fail.
- Both non-nil errors are children of the joined error.
- errors.Is traverses the joined error tree.
err=errors.Join(ErrWrite,ErrClose); inspect both with errors.Is
The fixture specifies values and relationships without claiming program execution.
Expected result: Both documented sentinel checks return true.
Why this result follows: A joined error can expose multiple intended categories.
14. Join nil
Problem: Join only nil errors in Go 1.22.
- There are no non-nil errors to combine.
- Join follows its documented all-nil behavior.
err=errors.Join(nil,nil)
The fixture specifies values and relationships without claiming program execution.
Expected result: err is nil.
Why this result follows: An all-nil join represents no combined error.
15. First As match
Problem: Join two *LimitError values with limits 3 then 7 and inspect with As.
- Traversal visits the first matching child before the second.
- As stops at the first matching error in the documented order.
err=errors.Join(&LimitError{Limit:3},&LimitError{Limit:7}); errors.As(err,&target)
The fixture specifies values and relationships without claiming program execution.
Expected result: target.Limit is 3.
Why this result follows: The first match in the error tree determines the extracted value.
16. Nil success
Problem: A successful operation returns literal nil as its error.
- The success path places no concrete error in the interface.
- The returned error interface has no dynamic type or value.
err=operationSuccess(); err==nil
The fixture specifies values and relationships without claiming program execution.
Expected result: err==nil is true.
Why this result follows: Literal nil represents a nil error interface.
17. Typed nil error
Problem: A function returns a nil *Problem as error even though *Problem implements error.
- The pointer value is nil but its concrete type is *Problem.
- The converted error interface retains that dynamic type.
var p *Problem; return p as error; compare err==nil
The fixture specifies values and relationships without claiming program execution.
Expected result: err==nil is false.
Why this result follows: A typed nil pointer does not create a nil interface.
18. Translate private error
Problem: A repository maps its private storage missing error to public ErrMissing.
- The public boundary selects its own documented category.
- The private storage identity is not wrapped into the returned tree.
private ErrDBMissing -> public ErrMissing; returned tree excludes private sentinel
The fixture specifies values and relationships without claiming program execution.
Expected result: errors.Is matches public ErrMissing but not ErrDBMissing.
Why this result follows: Translation prevents unintended dependence on the storage implementation.
19. Partial result
Problem: A reader returns a documented prefix and a non-nil error.
- The error reports that the complete operation failed.
- The contract explicitly permits use of the returned prefix.
result="AB"; err=ErrTruncated; contract permits partial prefix
The fixture specifies values and relationships without claiming program execution.
Expected result: The caller handles AB as partial data and also handles ErrTruncated.
Why this result follows: A documented partial result does not turn failure into success.
20. Publish concurrent error
Problem: A worker returns an error to its owner over a channel after completing work.
- The channel communication orders the send and corresponding receive.
- The owner inspects the received error after communication completes.
worker: err=work(); resultCh<-err; owner: err=<-resultCh
The fixture specifies values and relationships without claiming program execution.
Expected result: The owner can classify the received error under the API contract.
Why this result follows: The channel supplies synchronization for this handoff; wrapping alone does not.
Common errors and fixes
A wrapped cause is not found
- Why it happens
- The code compares error values directly.
- How to fix it
- Use errors.Is for an exposed wrapped cause.
- Prevent it next time
- Test wrapped errors.
A type assertion misses the cause
- Why it happens
- The desired error is inside a wrapper.
- How to fix it
- Use errors.As with the appropriate target.
- Prevent it next time
- Test type extraction through wrapping.
An internal cause becomes a compatibility burden
- Why it happens
- The API exposed an implementation detail unintentionally.
- How to fix it
- Define a deliberate public error contract.
- Prevent it next time
- Review wrapping at API boundaries.
20 practice exercises
1. Why can errors.Is find the sentinel in the first example?
Show hint
Follow the unwrap relationship created by %w.
Show worked solution
The %w verb wraps the sentinel, and errors.Is traverses the error chain to match it.
fmt.Errorf with %w creates a wrapper exposing the inner sentinel through an unwrap relationship. errors.Is starts at the outer error and traverses that relationship until it matches the same sentinel; it does not need to match message text.
Check your solution
- errors.Is finds the wrapped sentinel.
- An unrelated independent sentinel does not match merely because text is similar.
2. Why avoid classifying failures by message text?
Show hint
Separate human-facing text from the caller’s documented identity.
Show worked solution
Human-readable wording can change independently of the error identity. Use a documented error value or type instead.
Message wording can change while the failure category stays the same, and independent errors can share text. Use a documented sentinel with errors.Is or a documented type with errors.As, and keep sensitive details out of public output.
Check your solution
- The category decision uses documented identity or type rather than wording.
- An unrelated error does not match the category merely because its message is similar.
3. A package returns ErrMissing wrapped with two operation labels. A caller compares the outer error directly to ErrMissing. Repair the classification and test an unrelated error.
Show hint
Direct equality does not traverse wrappers.
Show worked solution
Use errors.Is(err, ErrMissing) when the package documents that identity, and branch on the result. Test a twice-wrapped missing error as a match and a separately wrapped ErrDenied as a nonmatch; keep human-readable context independent of the decision.
Check your solution
- The twice-wrapped missing error matches ErrMissing.
- The denied error does not match ErrMissing.
4. Two errors.New calls both use "quota". A service must retry only a documented ErrQuota category. Design the classification and a negative test.
Show hint
Identical messages do not make independent errors identical.
Show worked solution
Expose one package-owned ErrQuota sentinel or a documented type and use errors.Is or errors.As under that contract. Test a wrapped instance of the intended category and an independently created error with the same text; only the documented category should select the retry branch.
Check your solution
- The documented quota error selects the intended retry branch.
- An unrelated same-text error does not automatically match.
5. A public API wraps a private database sentinel with %w, but callers should depend only on a package-owned not-found category. Redesign the boundary.
Show hint
Wrapping exposes an inspectable identity.
Show worked solution
Translate the storage failure into a documented public ErrMissing category and retain private diagnostics separately under an access policy. Test that errors.Is finds ErrMissing while the returned tree does not expose the database sentinel, and verify an unrelated storage failure maps to its own documented outcome.
Check your solution
- The public missing category matches.
- The private sentinel is not exposed through the returned error tree.
6. A developer replaces %w with %v to avoid exposing a private cause, but its message contains a synthetic credential. Explain the remaining risk and test both identity and output.
Show hint
Text can still disclose information even without wrapping.
Show worked solution
Translate or redact the private detail before producing the public message; %v prevents that wrapping relationship but does not sanitize its text. Assert the private sentinel does not match through errors.Is and the synthetic credential is absent from the response and relevant logs under the chosen policy.
Check your solution
- The private sentinel is not exposed through the returned error tree.
- The synthetic credential is absent from the public output.
7. A caller needs the Limit field of a wrapped *LimitError, and also receives unrelated errors. Design a safe errors.As branch.
Show hint
Declare a target pointer and use its address.
Show worked solution
Declare var target *LimitError and call errors.As(err, &target); read Limit only when it returns true. In the false branch handle the unrelated error without dereferencing target. Use fresh target state per test and cover direct, wrapped and nonmatching cases.
Check your solution
- Direct and wrapped LimitError cases expose the documented Limit.
- The unrelated error takes the fallback without dereferencing target.
8. An API returns an outer wrapper containing *ParseError. A direct assertion to *ParseError fails. Explain why and design a test of the intended public type contract.
Show hint
A direct assertion examines the outer dynamic value.
Show worked solution
The wrapper’s dynamic type is not *ParseError, even though its child is. If the public API exposes that type, use errors.As through the wrapper and test the extracted fields; include an unrelated error that must not match. If it is private, expose a stable public category instead.
Check your solution
- The wrapped documented type is found with errors.As.
- An unrelated error does not match the target type.
9. A worker operation and cleanup both fail with documented ErrWrite and ErrClose. Design an error result that preserves both categories and states what happened to output.
Show hint
Combining errors does not undo partial external effects.
Show worked solution
Use errors.Join when both identities are intended to be public, and test errors.Is for each category. Separately record any accepted output prefix or committed state under the operation’s contract; do not describe the write as rolled back merely because both errors were returned.
Check your solution
- The joined error matches both documented categories.
- Partial output or state is reported separately from classification.
10. An operation succeeds and cleanup also succeeds. A helper calls errors.Join with two nil values. What should it return, and how should success be tested?
Show hint
Join discards nil inputs.
Show worked solution
errors.Join(nil, nil) returns nil, so the helper should expose a genuinely nil error interface for success. Assert err==nil through the declared return type, then use a separate fixture with one non-nil cleanup error to verify failure classification.
Check your solution
- The all-nil join yields a nil error.
- A non-nil cleanup error is not mistaken for success.
11. An error tree contains two matching *LimitError instances with limits 4 and 9. The first is visited first. What can errors.As return, and how should an API avoid relying on the other?
Show hint
As returns the first match in documented traversal order.
Show worked solution
As extracts the first matching LimitError, whose Limit is 4 in this stated tree. If callers need all limits, expose a separate documented aggregate representation or traversal API rather than assuming As selects the second. Test both tree order and the public requirement.
Check your solution
- The stated first-match fixture extracts limit 4.
- The API does not infer all matching limits from one As call.
12. A custom wrapper exposes Unwrap() []error. Its implementation returns a slice containing nil. Identify the contract issue and propose a test.
Show hint
Custom multi-error unwrap results must not contain nil elements.
Show worked solution
Construct the returned child slice from non-nil errors only, and return a valid empty or populated slice according to the wrapper design. Test Is against each intended child and an unrelated target, and inspect the returned children for nil. Avoid cycles in the wrapper graph.
Check your solution
- No child returned by Unwrap is nil.
- Intended child categories match and an unrelated target does not.
13. A function returns a nil *MyError as error on success. Explain the observed comparison and repair the success path.
Show hint
The interface retains the concrete dynamic type.
Show worked solution
If *MyError implements error, returning its nil pointer as error creates a non-nil interface with a nil dynamic pointer. Return literal nil on success instead. Test err==nil through the declared return type and verify a real error is non-nil.
Check your solution
- Success returns a genuinely nil error interface.
- A real failure returns a non-nil error.
14. An operation returns a partial result and ErrTruncated; the contract permits the prefix. Design caller behavior and tests.
Show hint
A partial value can be useful without making the operation successful.
Show worked solution
Inspect the error first, then retain or report the prefix only according to the documented partial-result policy while also handling ErrTruncated. Test the known prefix and matching category, plus a complete nil-error result and a failure whose result is not usable.
Check your solution
- The documented prefix is handled together with ErrTruncated.
- The complete result and unusable failure follow their distinct paths.
15. Two goroutines write an error variable concurrently while the owner reads it. Explain why wrapping does not make this safe and design a handoff.
Show hint
Error values do not synchronize surrounding shared state.
Show worked solution
Send each worker’s result over channels or protect a shared aggregate with a mutex, then wait for all workers before inspecting or joining errors. Define ordering or aggregation policy explicitly. Test completion and classification with coordinated workers; a supported race run provides evidence only for exercised paths.
Check your solution
- Workers publish errors through the chosen synchronization protocol.
- The owner waits for all required results before classification.
16. A custom error implements Is for a public temporary category. Design matching and nonmatching tests without comparing messages.
Show hint
Custom Is behavior must itself be part of the documented contract.
Show worked solution
Create a fixture whose Is method matches the documented ErrTemporary target and assert errors.Is returns true; assert a distinct ErrPermanent target returns false unless the implementation documents otherwise. Include a wrapped custom error to check traversal through the intended boundary.
Check your solution
- The documented temporary target matches directly and through the wrapper.
- The unrelated permanent target does not match.
17. A consumer test asserts the exact full wording of a wrapped error even though only its category is documented. Redesign the test and retain a privacy check.
Show hint
Separate contractual identity from incidental wording.
Show worked solution
Use errors.Is or errors.As for the documented category and verify any required contextual property without freezing incidental prose. Add a synthetic secret marker to the private cause and assert it is absent from the public response. Test a nonmatching error separately.
Check your solution
- The documented category is matched without exact full-message equality.
- The synthetic secret does not appear in public output.
18. A local fake verifies a wrapped sentinel, but the real remote adapter was not run. Draft a bounded evidence statement and next check.
Show hint
A fake establishes only the modeled boundary.
Show worked solution
Report the fake’s configured failure and observed classification, and mark the real adapter integration unrun. In an isolated environment, exercise a controlled adapter failure and verify the public category and redaction under the documented contract. Do not claim the remote service behaved as the fake.
Check your solution
- The report identifies the fake and unrun integration.
- The next check observes the adapter’s public category and safe output.
19. A library changes from exposing a documented backend sentinel to translating it into a new public category. Plan a compatibility transition and consumer tests.
Show hint
Callers may already branch on the old exposed identity.
Show worked solution
Inventory consumers using errors.Is on the backend sentinel, decide whether to preserve matching temporarily or introduce a documented breaking migration, and publish the new public category. Test old and new supported behaviors according to that plan and verify unrelated failures do not match.
Check your solution
- The transition states which error identities remain supported and when.
- Consumer tests cover the supported old and new classification paths.
20. An errors.As call passes a non-pointer target and panics. Explain the distinction between an invalid target and a valid nonmatching error, then repair it.
Show hint
As requires a valid non-nil pointer target of the documented shape.
Show worked solution
Declare a variable of the desired error type and pass its address, for example var target *LimitError followed by errors.As(err, &target). A valid target with no match returns false; an invalid target is a programming error that panics. Test a match and a nonmatch using the corrected target.
Check your solution
- The corrected target does not panic merely because the error is unrelated.
- A matching error populates the target and an unrelated one returns false.
Key takeaways
- Wrap only causes the API intends to expose.
- Use errors.Is and errors.As for intentional classification.
- Message text alone does not define error identity.
- Exposing a cause through wrapping can become a compatibility commitment.
- Wrapping an error does not synchronize surrounding shared state.