Created
June 6, 2018 09:59
-
-
Save mitsuhiko/1644310dd3db3cd87403d172e6c24110 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= 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