From b689bb92608a51c708edd764111702134e1cf788 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 6 Mar 2019 11:45:33 -0800 Subject: [PATCH] add failure support --- Cargo.toml | 9 +++++- src/error.rs | 78 +++++++++++++++------------------------------------- 2 files changed, 30 insertions(+), 57 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6493404dd..a0eb17312 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,11 +28,15 @@ name = "actix_http" path = "src/lib.rs" [features] -default = [] +default = ["fail"] # openssl ssl = ["openssl", "actix-connector/ssl"] +# failure integration. it is on by default, it will be off in future versions +# actix itself does not use failure anymore +fail = ["failure"] + [dependencies] #actix-service = "0.3.2" actix-codec = "0.1.0" @@ -77,6 +81,9 @@ trust-dns-resolver = { version="0.11.0-alpha.2", default-features = false } # openssl openssl = { version="0.10", optional = true } +# failure is optional +failure = { version = "0.1.5", optional = true } + [dev-dependencies] actix-rt = "0.2.0" #actix-server = { version = "0.3.0", features=["ssl"] } diff --git a/src/error.rs b/src/error.rs index cd5cabaa6..4762f27ff 100644 --- a/src/error.rs +++ b/src/error.rs @@ -66,28 +66,6 @@ impl Error { } } - // /// Attempts to downcast this `Error` to a particular `Fail` type by - // /// reference. - // /// - // /// If the underlying error is not of type `T`, this will return `None`. - // pub fn downcast_ref(&self) -> Option<&T> { - // // in the most trivial way the cause is directly of the requested type. - // if let Some(rv) = Fail::downcast_ref(self.cause.as_fail()) { - // return Some(rv); - // } - - // // in the more complex case the error has been constructed from a failure - // // error. This happens because we implement From by - // // calling compat() and then storing it here. In failure this is - // // represented by a failure::Error being wrapped in a failure::Compat. - // // - // // So we first downcast into that compat, to then further downcast through - // // the failure's Error downcasting system into the original failure. - // let compat: Option<&failure::Compat> = - // Fail::downcast_ref(self.cause.as_fail()); - // compat.and_then(|e| e.get_ref().downcast_ref()) - // } - /// Converts error to a response instance and set error message as response body pub fn response_with_message(self) -> Response { let message = format!("{}", self); @@ -96,28 +74,6 @@ impl Error { } } -// /// Helper trait to downcast a response error into a fail. -// /// -// /// This is currently not exposed because it's unclear if this is the best way -// /// to achieve the downcasting on `Error` for which this is needed. -// #[doc(hidden)] -// pub trait InternalResponseErrorAsFail { -// #[doc(hidden)] -// fn as_fail(&self) -> &Fail; -// #[doc(hidden)] -// fn as_mut_fail(&mut self) -> &mut Fail; -// } - -// #[doc(hidden)] -// impl InternalResponseErrorAsFail for T { -// fn as_fail(&self) -> &Fail { -// self -// } -// fn as_mut_fail(&mut self) -> &mut Fail { -// self -// } -// } - /// Error that can be converted to `Response` pub trait ResponseError: fmt::Debug + fmt::Display { /// Create response for error @@ -176,18 +132,6 @@ impl From for Error { } } -// /// Compatibility for `failure::Error` -// impl ResponseError for failure::Compat where -// T: fmt::Display + fmt::Debug + Sync + Send + 'static -// { -// } - -// impl From for Error { -// fn from(err: failure::Error) -> Error { -// err.compat().into() -// } -// } - /// Return `GATEWAY_TIMEOUT` for `TimeoutError` impl ResponseError for TimeoutError { fn error_response(&self) -> Response { @@ -1023,6 +967,28 @@ where InternalError::new(err, StatusCode::NETWORK_AUTHENTICATION_REQUIRED).into() } +#[cfg(feature = "fail")] +mod failure_integration { + use super::*; + use failure::{self, Fail}; + + /// Compatibility for `failure::Error` + impl ResponseError for failure::Compat + where + T: fmt::Display + fmt::Debug + Sync + Send + 'static, + { + fn error_response(&self) -> Response { + Response::new(StatusCode::INTERNAL_SERVER_ERROR) + } + } + + impl From for Error { + fn from(err: failure::Error) -> Error { + err.compat().into() + } + } +} + #[cfg(test)] mod tests { use super::*;