horacimacias
Re:Designing Error Types in Rust Libraries
on Thursday June 05, 2025 @02:42PM (#44194531)
nice article, thanks for sharing.
Regarding Solution 2, you can also use thiserror on the "custom built wrapper types", which eliminates the need to manually create the From implementations.
use thiserror::Error;
#[derive(Error, Debug)]
#[error(transparent)]
pub struct DbError(sqlx::Error);
#[derive(Error, Debug)]
#[error(transparent)]
pub struct HttpError(reqwest::Error);
#[derive(Error, Debug)]
pub enum MyPublicError {
#[error("database error: {0}")]
DbError(#[from] DbError),
#[error("http error: {0}")]
HttpError(#[from] HttpError),
}
fn main() -> Result<(), MyPublicError> {
do_db_stuff()?;
Ok(())
}
fn do_db_stuff() -> Result<(), DbError> {
todo!()
}