From 64ade803f96ce2e928b75e901c1447583e938050 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 25 Nov 2017 09:03:44 -0800 Subject: [PATCH] store error for error response --- src/error.rs | 2 +- src/httpresponse.rs | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/error.rs b/src/error.rs index fa06e5d40..ba7511279 100644 --- a/src/error.rs +++ b/src/error.rs @@ -64,7 +64,7 @@ impl fmt::Display for Error { /// `HttpResponse` for `Error` impl From for HttpResponse { fn from(err: Error) -> Self { - err.cause.error_response() + HttpResponse::from_error(err) } } diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 00e80da16..5adb32cbb 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -9,6 +9,7 @@ use http::header::{self, HeaderName, HeaderValue}; use Cookie; use body::Body; use route::Frame; +use error::Error; use encoding::ContentEncoding; /// Represents various types of connection @@ -33,6 +34,7 @@ pub struct HttpResponse { encoding: ContentEncoding, connection_type: Option, response_size: u64, + error: Option, } impl HttpResponse { @@ -58,9 +60,24 @@ impl HttpResponse { encoding: ContentEncoding::Auto, connection_type: None, response_size: 0, + error: None, } } + /// Constructs a error response + #[inline] + pub fn from_error(error: Error) -> HttpResponse { + let mut resp = error.cause().error_response(); + resp.error = Some(error); + resp + } + + /// The source `error` for this response + #[inline] + pub fn error(&self) -> Option<&Error> { + self.error.as_ref() + } + /// Get the HTTP version of this response. #[inline] pub fn version(&self) -> Option { @@ -355,19 +372,6 @@ impl HttpResponseBuilder { self } - /* /// Set response content charset - pub fn charset(&mut self, value: V) -> &mut Self - where HeaderValue: HttpTryFrom - { - if let Some(parts) = parts(&mut self.parts, &self.err) { - match HeaderValue::try_from(value) { - Ok(value) => { parts.headers.insert(header::CONTENT_TYPE, value); }, - Err(e) => self.err = Some(e.into()), - }; - } - self - }*/ - /// Set a cookie pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self { if let Some(parts) = parts(&mut self.parts, &self.err) { @@ -416,6 +420,7 @@ impl HttpResponseBuilder { encoding: parts.encoding, connection_type: parts.connection_type, response_size: 0, + error: None, }) }