1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-31 17:07:01 +02:00

refactor error handling

This commit is contained in:
Nikolay Kim
2017-11-15 20:06:28 -10:00
parent c565965865
commit de71ad7de4
18 changed files with 317 additions and 325 deletions

View File

@@ -1,6 +1,5 @@
//! Pieces pertaining to the HTTP message protocol.
//! Pieces pertaining to the HTTP response.
use std::{io, mem, str, fmt};
use std::error::Error as Error;
use std::convert::Into;
use cookie::CookieJar;
@@ -33,7 +32,6 @@ pub struct HttpResponse {
chunked: bool,
encoding: ContentEncoding,
connection_type: Option<ConnectionType>,
error: Option<Box<Error>>,
response_size: u64,
}
@@ -59,35 +57,10 @@ impl HttpResponse {
chunked: false,
encoding: ContentEncoding::Auto,
connection_type: None,
error: None,
response_size: 0,
}
}
/// Constructs a response from error
#[inline]
pub fn from_error<E: Error + 'static>(status: StatusCode, error: E) -> HttpResponse {
HttpResponse {
version: None,
headers: Default::default(),
status: status,
reason: None,
body: Body::from_slice(error.description().as_ref()),
chunked: false,
encoding: ContentEncoding::Auto,
connection_type: None,
error: Some(Box::new(error)),
response_size: 0,
}
}
/// The `error` which is responsible for this response
#[inline]
#[cfg_attr(feature="cargo-clippy", allow(borrowed_box))]
pub fn error(&self) -> Option<&Box<Error>> {
self.error.as_ref()
}
/// Get the HTTP version of this response.
#[inline]
pub fn version(&self) -> Option<Version> {
@@ -241,9 +214,6 @@ impl fmt::Debug for HttpResponse {
let _ = write!(f, " {:?}: {:?}\n", key, vals[0]);
}
}
if let Some(ref err) = self.error {
let _ = write!(f, " error: {}\n", err);
}
res
}
}
@@ -445,7 +415,6 @@ impl HttpResponseBuilder {
chunked: parts.chunked,
encoding: parts.encoding,
connection_type: parts.connection_type,
error: None,
response_size: 0,
})
}