Rust Error Handling

  • eyre is a fork of anyhow focused on better error reporting
  • Snafu
    • This is a comprehensive library with a lot of options. The big advantage here is that a lot of care is taken to make rich context information easy to use.
    • #[derive(Debug, Snafu)]
      enum Error {
      #[snafu(display("While doing {}", operation))]
      ErrorType {
        source: AnError,
        backtrace: Backtrace,
        operation: String,
    • This generates an ErrorType structure that can be used to add content to an AnError error. ErrorType contains everything except the source and backtrace fields. This can then be used with the context trait.
      • The context type also uses generics parameterized with Into<OriginalType>, so you don't have to do things like to_string() all over the place.
    • do_it_or_anerror().context(ErrorType{ operation: "doing it" })
    • Source fields can be mapped using #[snafu(source(from(ErrorType, MapFunc)))]
    • Applications may want to enable the backtraces-impl-backtrace-crate feature to capture backtraces. The unstable-backtraces-impl-std feature does the same for Rust's built-in backtraces, but those aren't stabilized yet.
    • The futures feature adds context methods to futures and streams that return Results.