My goal is to use Anyhow::Result
in everything except my Actix request handlers, then ideally convert the error types automatically. Here's my code:
fn map_error<V, E, F, RE>(r: Result<V, E>, error_constructor: F, message: &str, status_code: u16) -> Result<V>
where E: std::fmt::Debug,
RE: Into<actix_web::Error>,
F: Fn(String, StatusCode) -> RE
{
r.map_err(|err| {
error!("{:?}", err);
error_constructor(
message.to_string(),
StatusCode::from_u16(status_code).expect("bad status code")
).into()
})
}
pub async fn login(
session: Session,
app_state: web::Data<AppState>,
item: web::Json<LoginRequest>,
) -> Result<web::Json<ApiUser>> {
let conn = map_error(app_state.db.get(), error::InternalError::new, "Could not get database connection", 500)?;
let user = map_error(session::authenticate_user(&conn, &item.email, &item.password), error::InternalError::new, "Couldn't authenticate user", 401)?;
Ok(web::Json(ApiUser { email: user.email }))
}
Here's a possibly relevant thread (not sure) about using derive_more
: Convert from `derive_more` to `thiserror` by otavio · Pull Request #1412 · actix/actix-web · GitHub
I feel like I'm ice-skating uphill. I suspect that the endgame for what I'm doing here is recapitulating all of the HTTP error types through this function. How do y'all handle this?