-
Notifications
You must be signed in to change notification settings - Fork 138
How to impl a trait for failure::Error? #104
Comments
Yes, if you need to implement a trait for it, you'll need to use a newtype around it. :-\ If you do that, I recommend adding |
Hm... it's a bit complicated :( #[derive(Debug)]
enum AppError<'a>{
Fail(&'a failure::Error),
Unknown
}
type Result<'a, T> = result::Result<Json<T>, AppError<'a>>; with impl<'a, F: failure::Fail> From<F> for AppError<'a> {
fn from(f: F) -> AppError<'a> {
if let Some(err) = f.downcast_ref::<failure::Error>() {
AppError::Fail(err)
} else{
AppError::Unknown
}
}
} but this leads to
And implementing the |
The best way to convert your error type to struct AppError(failure::Error);
impl<F: failure::Fail> From<F> for AppError {
fn from(f: F) -> AppError { AppError(failure::Error::from(f)) }
} You should also implement the conversion for impl From<failure::Error> for AppError {
fn from(e: failure::Error) -> AppError { AppError(e) }
} Together, these will let you |
I'm sorry for bothering you:
|
Ugh that's very unfortunate! I'm sorry, I thought it would work, but coherence has bitten us pretty badly here. |
I've got a better solution! If you want to abstract over both In your case: impl<T: Into<failure::Error>> From<T> for AppError {
fn from(t: T) -> AppError { AppError(t.into()) }
} This should work for you! |
Ok, your last hint solved most issues but now I still have problems to |
Slightly off topic, but this last hint has let me refactor how my newtype impl's were structured in https://github.com/tismith/exitfailure - I was running into the same coherence issues as @flosse and using |
Any chance we get |
After 3 days of trying to coalesce errors between tokio, thrussh, and failure I'm pretty much ready to give up in frustration. It seems like if the story for failure is backwards compatibility then not implementing std::error::Error is a pretty big hole. Thanks. |
I trying to migrate from
quick_error
tofailure
with the following code.This was the original error enum:
that is used within a rocket module:
And here I was able to impl a trait for my custom
AppError
:With
failure
I don't need the wrappingAppError
anymore (👍) so now theResult
looks like this: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 thestd::error::Error
manually?The text was updated successfully, but these errors were encountered: