1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 23:51:06 +01:00

store error for error response

This commit is contained in:
Nikolay Kim 2017-11-25 09:03:44 -08:00
parent 940bc08aba
commit 64ade803f9
2 changed files with 19 additions and 14 deletions

View File

@ -64,7 +64,7 @@ impl fmt::Display for Error {
/// `HttpResponse` for `Error` /// `HttpResponse` for `Error`
impl From<Error> for HttpResponse { impl From<Error> for HttpResponse {
fn from(err: Error) -> Self { fn from(err: Error) -> Self {
err.cause.error_response() HttpResponse::from_error(err)
} }
} }

View File

@ -9,6 +9,7 @@ use http::header::{self, HeaderName, HeaderValue};
use Cookie; use Cookie;
use body::Body; use body::Body;
use route::Frame; use route::Frame;
use error::Error;
use encoding::ContentEncoding; use encoding::ContentEncoding;
/// Represents various types of connection /// Represents various types of connection
@ -33,6 +34,7 @@ pub struct HttpResponse {
encoding: ContentEncoding, encoding: ContentEncoding,
connection_type: Option<ConnectionType>, connection_type: Option<ConnectionType>,
response_size: u64, response_size: u64,
error: Option<Error>,
} }
impl HttpResponse { impl HttpResponse {
@ -58,9 +60,24 @@ impl HttpResponse {
encoding: ContentEncoding::Auto, encoding: ContentEncoding::Auto,
connection_type: None, connection_type: None,
response_size: 0, 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. /// Get the HTTP version of this response.
#[inline] #[inline]
pub fn version(&self) -> Option<Version> { pub fn version(&self) -> Option<Version> {
@ -355,19 +372,6 @@ impl HttpResponseBuilder {
self self
} }
/* /// Set response content charset
pub fn charset<V>(&mut self, value: V) -> &mut Self
where HeaderValue: HttpTryFrom<V>
{
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 /// Set a cookie
pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self { pub fn cookie<'c>(&mut self, cookie: Cookie<'c>) -> &mut Self {
if let Some(parts) = parts(&mut self.parts, &self.err) { if let Some(parts) = parts(&mut self.parts, &self.err) {
@ -416,6 +420,7 @@ impl HttpResponseBuilder {
encoding: parts.encoding, encoding: parts.encoding,
connection_type: parts.connection_type, connection_type: parts.connection_type,
response_size: 0, response_size: 0,
error: None,
}) })
} }