1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-23 15: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`
impl From<Error> for HttpResponse {
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 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<ConnectionType>,
response_size: u64,
error: Option<Error>,
}
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<Version> {
@ -355,19 +372,6 @@ impl HttpResponseBuilder {
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
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,
})
}