From f33c489154bf98ca4fae2555f3c90f92b2d55363 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 24 Nov 2017 10:03:13 -0800 Subject: [PATCH] added default ErrorResponse for std::error::Error --- Cargo.toml | 3 +++ src/error.rs | 21 +++++++++++++++------ src/lib.rs | 4 ++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d7c55f765..40515fb1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/error.rs b/src/error.rs index d2808533b..8ad17daa5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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 for HttpResponse { } } +/// `Error` for any error that implements `ErrorResponse` impl From for Error { fn from(err: T) -> Error { Error { cause: Box::new(err) } } } -// /// Default error is `InternalServerError` -// impl 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 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)] diff --git a/src/lib.rs b/src/lib.rs index c0463aee4..cbb30a950 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;