mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-28 09:42:40 +01:00
add failure support
This commit is contained in:
parent
ad08e856d7
commit
b689bb9260
@ -28,11 +28,15 @@ name = "actix_http"
|
|||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = ["fail"]
|
||||||
|
|
||||||
# openssl
|
# openssl
|
||||||
ssl = ["openssl", "actix-connector/ssl"]
|
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]
|
[dependencies]
|
||||||
#actix-service = "0.3.2"
|
#actix-service = "0.3.2"
|
||||||
actix-codec = "0.1.0"
|
actix-codec = "0.1.0"
|
||||||
@ -77,6 +81,9 @@ trust-dns-resolver = { version="0.11.0-alpha.2", default-features = false }
|
|||||||
# openssl
|
# openssl
|
||||||
openssl = { version="0.10", optional = true }
|
openssl = { version="0.10", optional = true }
|
||||||
|
|
||||||
|
# failure is optional
|
||||||
|
failure = { version = "0.1.5", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
actix-rt = "0.2.0"
|
actix-rt = "0.2.0"
|
||||||
#actix-server = { version = "0.3.0", features=["ssl"] }
|
#actix-server = { version = "0.3.0", features=["ssl"] }
|
||||||
|
78
src/error.rs
78
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<T: Fail>(&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<failure::Error> 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<failure::Error>> =
|
|
||||||
// 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
|
/// Converts error to a response instance and set error message as response body
|
||||||
pub fn response_with_message(self) -> Response {
|
pub fn response_with_message(self) -> Response {
|
||||||
let message = format!("{}", self);
|
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<T: ResponseError> 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`
|
/// Error that can be converted to `Response`
|
||||||
pub trait ResponseError: fmt::Debug + fmt::Display {
|
pub trait ResponseError: fmt::Debug + fmt::Display {
|
||||||
/// Create response for error
|
/// Create response for error
|
||||||
@ -176,18 +132,6 @@ impl<T: ResponseError + 'static> From<T> for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// /// Compatibility for `failure::Error`
|
|
||||||
// impl<T> ResponseError for failure::Compat<T> where
|
|
||||||
// T: fmt::Display + fmt::Debug + Sync + Send + 'static
|
|
||||||
// {
|
|
||||||
// }
|
|
||||||
|
|
||||||
// impl From<failure::Error> for Error {
|
|
||||||
// fn from(err: failure::Error) -> Error {
|
|
||||||
// err.compat().into()
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// Return `GATEWAY_TIMEOUT` for `TimeoutError`
|
/// Return `GATEWAY_TIMEOUT` for `TimeoutError`
|
||||||
impl<E: ResponseError> ResponseError for TimeoutError<E> {
|
impl<E: ResponseError> ResponseError for TimeoutError<E> {
|
||||||
fn error_response(&self) -> Response {
|
fn error_response(&self) -> Response {
|
||||||
@ -1023,6 +967,28 @@ where
|
|||||||
InternalError::new(err, StatusCode::NETWORK_AUTHENTICATION_REQUIRED).into()
|
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<T> ResponseError for failure::Compat<T>
|
||||||
|
where
|
||||||
|
T: fmt::Display + fmt::Debug + Sync + Send + 'static,
|
||||||
|
{
|
||||||
|
fn error_response(&self) -> Response {
|
||||||
|
Response::new(StatusCode::INTERNAL_SERVER_ERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<failure::Error> for Error {
|
||||||
|
fn from(err: failure::Error) -> Error {
|
||||||
|
err.compat().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
Loading…
Reference in New Issue
Block a user