[Solved] Failure and std::error::Error (warp related)

Hi,

I want to use failure in my web server using warp as the framewor:

#[derive(Debug, Clone, Fail, Serialize)]
pub struct ServerError {
  pub status: i32,
  pub msg: String,
}

However, warp is expecting that the trait std::error::Error to be implemented in order to use warp::Rejection::find_cause.

I tried to implement the trait but I got conflict between failure and std::error::Error:

error[E0119]: conflicting implementations of trait `failure::Fail` for type `error::ServerError`:
 --> src\error.rs:9:24
  |
9 | #[derive(Debug, Clone, Fail, Serialize)]
  |                        ^^^^
  |
  = note: conflicting implementation in crate `failure`:
          - impl<E> failure::Fail for E
            where E : 'static, E: std::error::Error, E: std::marker::Send, E: std::marker::Sync;

To solve this, I was going to create a mirror type StdServerError and an impl From<ServerError> for it, but it feels awkward.

Is there any more elegant solution here?

Both Fail and Error provide a method compat() that wraps your failure-error in a newtype that implements std::error::Error. You could wrap your error in this manner before passing it on to warp.

1 Like

Thank you so much!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.