Why Use Structured Errors in Rust Applications?

Going against the common wisdom of “using anyhow for applications”.

Dmitrii Aleksandrov

I was struggeling to report the source cause of an error in a #rust codebase!

The #thiserror crate and #miette crate seemed to be the right set of tools for this.
Although it hasn't been easy to figure out!
( but I didn't even cry 😏)

Once you master both, error reporting in #rust becomes fun, standardized, colorful and painless!

Interesting read about #snafu vs #thiserror: https://medium.com/@greptime_team/error-handling-for-large-rust-projects-a-deep-dive-into-5e10ee4cbc96

The stack_trace_debug macro proposed reminds me strongly of the error_chain construct in chapter 8 of @algo_luca's Zero To Production In Rust. https://www.zero2prod.com/index.html

#RustLang #ErrorHandling

Error Handling for Large Rust Projects — A Deep Dive into GreptimeDB’s Practices

Rust’s error handling is centered around the Result<T, E> enum, where E typically (but unnecessarily) extends std::error::Error. This blog shares our experience organizing variant types of Error in a…

Medium

@greysemanticist It works, because thiserror generates a From impl for rusqlite::Error for your AuthError (due to the #[from] attribute).

And ? will use that From impl to return AuthError.

See also the std #Rust docs on how ? works:

"When applied to values of the Result<T, E> type, it propagates errors. If the value is Err(e), then it will return Err(From::from(e)) from the enclosing function or closure."

https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator

#thiserror and ? can look like magic, but it's just #RustLang

Operator expressions - The Rust Reference