The error handling project group aims to reduce confusion on how to structure error handling for users in the rust community. This will be accomplished by creating learning resources and pushing effort to upstream widely used crates into std. As a secondary goal this project group will also try to resolve some known issues with the Error trait and reporting errors in panics/termination.
- recoverable error => an error that can reasonably be expected to be encountered e.g. a missing file
- unrecoverable error => an error that cannot reasonably be expected to happen and which indicates a bug e.g. indexing out of bounds
- Error Type => A type that represents an recoverable error. Error types can optionally implement the error trait so that it can be reported to the user or be converted into a trait object.
- Reporting Type => A type that can store all recoverable errors an application may need to propogate and print them as error reports
- reporting types can represent the recoverable errors either via concrete types, likely paramaterized, or trait objects.
- reporting types often bundle context with errors when they are constructed, e.g.
Backtrace
- reporting types often provide helper functions for creating ad hoc errors whose only purpose is to be reported e.g.
anyhow::format_err!
oreyre::WrapErr
Here is a tenative starting point, subject to change:
- use
Result
andError
types for recoverable errors - use
panic
for unrecoverable errors - impl
Error
for error types that may need to be reported to a human or be composed with other errors - use enums for types representing multiple failure cases that may need to be handled
- for libraries often times you want to support both reporting and handling so you impl Error on a possibly non_exhaustive enum
- error kind pattern for associating context with every enum variant without including the member in every enum variant
- convert to a reporting type when the error is no longer expected to be handled beyond reporting e.g.
anyhow::Error
oreyre::Report
or when trait object + downcast error handling is preferable - Recommend boxing concrete error types when stack size is an issue rather than boxing and converting to dyn Errors
- What is the concensus on handling
dyn Errors
? Should it be encouraged or discouraged? Should we look into makingBox<dyn Error...>
impl Error?
- Document the concensus
- Communicate plan for future changes to error handling, and the libraries that future changes are being based off of
- Produce learning resources related to current best practices
- new chapters in the book?
- Survey the current libraries in the ecosystem
- anyhow
- eyre
- Evaluate value of features included
- single word width on stack
- error wrapping with display types and with special downcast support
- report hook + configurable dyn ReportHandler type for custom report formats and content, similar to panic handler but for errors.
- libcore compatibility
- provide a derive macro for Error in std
- Stabilize the
Backtrace
type but possibly notfn backtrace
on the Error trait- provide necessary api on
Backtrace
to support crates likecolor-backtrace
- provide necessary api on
- move error to core
- depends on generic member access
- requires resolving downcast dependency on Box and blocking the stabilization of fn backtrace
- potentially stabilize an error Reporting type based on
anyhow
andeyre
now that they're close to having identical feature sets
- Generic member access on the error trait
- error return traces
- depends on specialization and generic member access
- fix rough corners around reporting errors and
Termination
- This group should not be involved in design discussions for the
Try
trait,try
blocks, ortry
fns
- Group membership is open, any interested party can participate in discussions, repeat contributors will be added appropriate teams.
I'm not sure, my main concern is getting prompt feedback on RFCs.
Error handling has historically been handled via community effort which has resulted a great deal of competing and outdated solutions and resources. An officially supported project group has the best chance of writing and maintaining up to date resources that would reach a wide audience.
The project group will create RFCs for various changes to the standard library and the team will review them via the standard RFC process.
Jane Lusby, Andrew Gallant, and Sean Chen
temporary
This depends pretty heavily on how quickly the RFCs move, anywhere between 6 months and 2 years I'd guess but don't quote me on this.
Primarily the libs team, but there may be some small interactions with the lang team, compiler team, and traits working group.
Primarily in drafting RFCs, writing is not this author's strong suit.