Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created June 6, 2018 09:59
Show Gist options
  • Save mitsuhiko/1644310dd3db3cd87403d172e6c24110 to your computer and use it in GitHub Desktop.
Save mitsuhiko/1644310dd3db3cd87403d172e6c24110 to your computer and use it in GitHub Desktop.
/// Helper trait to downcast a response error into a fail.
pub trait ResponseErrorAsFail {
/// Returns the response error as fail.
fn as_fail(&self) -> &Fail;
/// Returns the response error as mut fail.
fn as_mut_fail(&mut self) -> &mut Fail;
}
#[doc(hidden)]
impl<T: ResponseError> ResponseErrorAsFail for T {
fn as_fail(&self) -> &Fail { self }
fn as_mut_fail(&mut self) -> &mut Fail { self }
}
/// Error that can be converted to `HttpResponse`
pub trait ResponseError: Fail + ResponseErrorAsFail {
/// Create response for error
///
/// Internal server error is generated by default.
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR)
}
}
impl ResponseError {
pub fn downcast_ref<T: Fail>(&self) -> Option<&T> {
Fail::downcast_ref(self.as_fail())
}
pub fn downcast_mut<T: Fail>(&mut self) -> Option<&mut T> {
self.as_mut_fail().downcast_mut()
}
// THIS ONE FAILS
pub fn root_cause(&self) -> &Fail {
self.as_fail().root_cause()
}
}
= note: candidate #1 is defined in an impl for the type `failure::Fail + 'static`
note: candidate #2 is defined in the trait `failure::Fail`
--> /Users/mitsuhiko/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.1/src/lib.rs:152:5
|
152| / fn root_cause(&self) -> &Fail where
153| | Self: Sized,
154| | {
155| | find_root_cause(self)
156| | }
| |_____^
= help: to disambiguate the method call, write `failure::Fail::root_cause(...)` instead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment