Adventures of the Retired Guy

Adventures of the Retired Guy

Stuff and nonsense from a retired guy

09 Nov 2023

Comprehensive error handling

Error handling in Rust is "some assembly required" -- error detail is available, and error handling models (early exit, bubble up, ignore) are supported, but there is not a well-accepted and complete convention for handling lower-level errors and bubbling u your own.

State of play

Best statement of currently recommended technique

  • .map_err() and .map_ok()
  • use this_error with some wrinkles

Examples of gaps in error model

tbs

To handle

  • a standard error type for all your code

    • Capture arbitrary detail from lower level error
    • Cascadable, so if you return the type, your caller can do likewise
  • deal with lower level crates that return crate-specific error type

  • Support all the error handling possibilities:

    • anticipate specific error return and process it as an alternative success case
    • capture detail from error, add your own consequence of failure, bubble up to caller
    • use '?
    • use Err(Box (dyn Error)) (if Error trait has enough power)