Skip to content
This repository was archived by the owner on Aug 16, 2021. It is now read-only.
This repository was archived by the owner on Aug 16, 2021. It is now read-only.

How to impl a trait for failure::Error? #104

Open
@flosse

Description

@flosse

I trying to migrate from quick_error to failure with the following code.
This was the original error enum:

quick_error!{
    #[derive(Debug)]
    pub enum AppError {
        Business(err: BusinessError){
            from()
            cause(err)
            description(err.description())
        }
        R2d2(err: r2d2::Error){
            from()
        }
        // ...
        Other(err: Box<error::Error + Send + Sync>){
            from()
            description(err.description())
            from(err: io::Error) -> (Box::new(err))
        }
    }
}

that is used within a rocket module:

type Result<T> = result::Result<Json<T>, AppError>;

#[get("/count/tags")]
fn get_count_tags(db: State<DbPool>) -> Result<usize> {
    let tags = usecase::get_tag_ids(&*db.get()?)?;
    Ok(Json(tags.len()))
}

And here I was able to impl a trait for my custom AppError:

impl<'r> Responder<'r> for AppError {
    fn respond_to(self, _: &rocket::Request) -> result::Result<Response<'r>, Status> {
        Err(match self {
            AppError::Business(ref err) => {
              // ...
            }
            _ => Status::InternalServerError,
        })
    }
}

With failure I don't need the wrapping AppError anymore (👍) so now the Result looks like this:

type Result<T> = result::Result<Json<T>, failure::Error>;

And everything is fine but of course I can't implement a trait for the external failure::Error :(
How would you solve this? Keep using the wrapper AppError that holds all possible error types?
Or use a struct WrappedError(failure::Error) and impl the std::error::Error manually?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions