Minimal Boilerplate for transitive Error `From` conversion using '?' syntax sugar

I've got some code like this (which doesn't work) (rocket and anyhow crates used, since this is my actual issue):

#[get("/")]
fn route() -> Result<&'static str, rocket::response::Debug<anyhow::Error>> {
    various()?;
    different()?;
    errors()?;
    can()?;
    happen()?;
    here()?;
    Ok("response")
}

This would be no problem if the Error type in the Result is only anyhow::Error
rocket::response::Debug however implements the From trait to convert arbitrary types that implement the Debug trait.

My current idea to keep it simple is using a lambda like this:

#[get("/")]
fn route() -> Result<&'static str, rocket::response::Debug<anyhow::Error>> {
    || -> Result<_, anyhow::Error> {
        various()?;
        different()?;
        errors()?;
        can()?;
        happen()?;
        here()?;
    }()?;
    Ok("response")
}

But this has its own caveats and is not really ideal, is there a better solution that has a similar amount of boilerplate?

I had the same problem for an issue earlier today. You can see my solution here.

Thanks, AFAIK this is similar to my solution isn't it?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.