1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 09:42:40 +01:00

Allow returning failure::Error from handlers (#65)

This implements From<failure::Error> for Error (by way of `failure::Compat`)
and ResponseError for failure::Compat<T>.
This commit is contained in:
Christopher Armstrong 2018-02-06 10:26:50 -06:00 committed by GitHub
parent b6d5516e3a
commit 884ea02f5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,7 @@ use std::error::Error as StdError;
use cookie; use cookie;
use httparse; use httparse;
use futures::Canceled; use futures::Canceled;
use failure;
use failure::{Fail, Backtrace}; use failure::{Fail, Backtrace};
use http2::Error as Http2Error; use http2::Error as Http2Error;
use http::{header, StatusCode, Error as HttpError}; use http::{header, StatusCode, Error as HttpError};
@ -95,6 +96,16 @@ impl<T: ResponseError> From<T> for 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()
}
}
/// Default error is `InternalServerError` /// Default error is `InternalServerError`
#[cfg(actix_nightly)] #[cfg(actix_nightly)]
default impl<T: StdError + Sync + Send + 'static> ResponseError for T { default impl<T: StdError + Sync + Send + 'static> ResponseError for T {
@ -651,11 +662,13 @@ pub fn ErrorInternalServerError<T>(err: T) -> InternalError<T> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::env;
use std::error::Error as StdError; use std::error::Error as StdError;
use std::io; use std::io;
use httparse; use httparse;
use http::{StatusCode, Error as HttpError}; use http::{StatusCode, Error as HttpError};
use cookie::ParseError as CookieParseError; use cookie::ParseError as CookieParseError;
use failure;
use super::*; use super::*;
#[test] #[test]
@ -784,4 +797,18 @@ mod tests {
from!(httparse::Error::TooManyHeaders => ParseError::TooLarge); from!(httparse::Error::TooManyHeaders => ParseError::TooLarge);
from!(httparse::Error::Version => ParseError::Version); from!(httparse::Error::Version => ParseError::Version);
} }
#[test]
fn failure_error() {
const NAME: &str = "RUST_BACKTRACE";
let old_tb = env::var(NAME);
env::set_var(NAME, "0");
let error = failure::err_msg("Hello!");
let resp: Error = error.into();
assert_eq!(format!("{:?}", resp), "Compat { error: ErrorMessage { msg: \"Hello!\" } }\n\n");
match old_tb {
Ok(x) => env::set_var(NAME, x),
_ => env::remove_var(NAME),
}
}
} }