Devy was my first moderately-sized Rust application (~26000 loc). While working on it, I discovered a technique that I don’t believe has an official name. I call it “Error Domains”.
Other people have used this same technique. Jeremy Chone covers this technique on his YouTube channel in his video on error handling best practices.
While Chone’s video covers errors in Rust more broadly, I want to focus in on the value of defining errors for each domain in your application and explicitly programming how they translate to one-another.
An Example
To illustrate “Error Domains”, I created an example repository Error Domain Example.
It is an API where a user can deposit and withdraw tokens from an account. This application
is divided into four modules or “domains”: api
, store
, auth
, and account
.
Each domain defines its own Error
enum. This requires the developer to identify the possible
errors that can occur within the domain.
Keep custom Result
within the crate. Only Error
is exposed.