• 0 Posts
  • 1 Comment
Joined 1 year ago
cake
Cake day: June 25th, 2025

help-circle
  • Depending on a usecase you might find that many of your “entrypoint functions” might all share the same error type. For example, if you’re making an API backend it’s likely gonna be something a status code and a message.

    In that case a simple way to avoid having to pattern match every single error is to implement From<DownstreamErrorType> for MyErrorEnum, and just use the ? operator. Example:

    fn do_something_with_database() -> Result<Whatever, DatabaseError> { /* ... */ }
    
    impl From<DatabaseError> for MyErrorType { /* ... */ }
    
    // this would be your function
    fn api_get_something() -> Result<SomeOtherType, MyErrorType> {
        let data: Whatever = do_something_with_database()?;
        // ...
        Ok(/* ... */)
    }
    

    EDIT: this is mentioned in the article already