1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-06-02 13:29:24 +00:00

added default ErrorResponse for std::error::Error

This commit is contained in:
Nikolay Kim 2017-11-24 10:03:13 -08:00
parent 5529ea0428
commit f33c489154
3 changed files with 22 additions and 6 deletions

View file

@ -32,6 +32,9 @@ tls = ["native-tls", "tokio-tls"]
# openssl
alpn = ["openssl", "openssl/v102", "openssl/v110", "tokio-openssl"]
# nightly
nightly = []
[dependencies]
log = "0.3"
failure = "0.1"

View file

@ -4,12 +4,16 @@ use std::str::Utf8Error;
use std::string::FromUtf8Error;
use std::io::Error as IoError;
#[cfg(feature="nightly")]
use std::error::Error as StdError;
use cookie;
use httparse;
use failure::Fail;
use http2::Error as Http2Error;
use http::{header, StatusCode, Error as HttpError};
use http_range::HttpRangeParseError;
use serde_json::error::Error as JsonError;
// re-exports
pub use cookie::{ParseError as CookieParseError};
@ -64,18 +68,23 @@ impl From<Error> for HttpResponse {
}
}
/// `Error` for any error that implements `ErrorResponse`
impl<T: ErrorResponse> From<T> for Error {
fn from(err: T) -> Error {
Error { cause: Box::new(err) }
}
}
// /// Default error is `InternalServerError`
// impl<T: StdError + Sync + Send + 'static> ErrorResponse for T {
// fn error_response(&self) -> HttpResponse {
// HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Body::Empty)
// }
// }
/// Default error is `InternalServerError`
#[cfg(feature="nightly")]
default impl<T: StdError + Sync + Send + 'static> ErrorResponse for T {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Body::Empty)
}
}
/// `InternalServerError` for `JsonError`
impl ErrorResponse for JsonError {}
/// A set of errors that can occur during parsing HTTP streams
#[derive(Fail, Debug)]

View file

@ -1,5 +1,9 @@
//! Web framework for [Actix](https://github.com/actix/actix)
#![cfg_attr(feature="nightly", feature(
specialization, // for impl ErrorResponse for std::error::Error
))]
#[macro_use]
extern crate log;
extern crate time;