proposal: Go 2: add a Causer interface for errors #27020
Labels
error-handling
Language & library change proposals that are about error handling.
FrozenDueToAge
Proposal
v2
An incompatible library change
Milestone
This proposal is for adding a new Causer interface for errors to the standard library to address the problem of error propagation through multiple packages. (This idea is inspired by the causer interface in the https://github.com/pkg/errors package.)
Consider three packages A, B, and C, where package A calls a function in package B and that function calls a function in package C. The general problem that this proposal addresses is how should package B propagate an error from package C back to package A.
There are three ways B could handle an error returned from C:
a. extracting some information from the error (e.g.
fmt.Errorf("blah blah: %s", err)
)b. embedding the original error (e.g. errors.Wrap).
Approach 1 neglects to add context, which could make it hard for package A to interpret the error. Approach 2a includes context from package B, but partially destroys information from the original error. Approach 2b is the most versatile, allowing package B to provide context while preserving the original error.
The problem with approach 2a currently is that there is no standard way to embed an error, and this is really something that benefits from a blessed interface that the Go ecosystem can then agree to use.
The interface is simple:
Example usage:
When I was almost done writing this, I found #25675 which appears to be suggesting something similar. However, this proposal is drastically narrower in scope (just about the Causer interface) and the rationale is that this really needs to be standardized to be useful since it is a problem with how errors cross package boundaries.
The text was updated successfully, but these errors were encountered: