1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-09-01 01:16:59 +02:00

simplify http response construction; deprecate httpcodes

This commit is contained in:
Nikolay Kim
2018-03-30 23:07:33 -07:00
parent 8d8f6bedad
commit 44e3df82f6
58 changed files with 561 additions and 539 deletions

View File

@@ -20,11 +20,9 @@ pub use url::ParseError as UrlParseError;
// re-exports
pub use cookie::{ParseError as CookieParseError};
use body::Body;
use handler::Responder;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use httpcodes::{self, HttpExpectationFailed};
/// A specialized [`Result`](https://doc.rust-lang.org/std/result/enum.Result.html)
/// for actix web operations
@@ -55,7 +53,7 @@ pub trait ResponseError: Fail {
///
/// Internal server error is generated by default.
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Body::Empty)
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR)
}
}
@@ -113,7 +111,7 @@ impl ResponseError for UrlParseError {}
/// Return `BAD_REQUEST` for `de::value::Error`
impl ResponseError for DeError {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
@@ -127,11 +125,11 @@ impl ResponseError for io::Error {
fn error_response(&self) -> HttpResponse {
match self.kind() {
io::ErrorKind::NotFound =>
HttpResponse::new(StatusCode::NOT_FOUND, Body::Empty),
HttpResponse::new(StatusCode::NOT_FOUND),
io::ErrorKind::PermissionDenied =>
HttpResponse::new(StatusCode::FORBIDDEN, Body::Empty),
HttpResponse::new(StatusCode::FORBIDDEN),
_ =>
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR, Body::Empty)
HttpResponse::new(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
@@ -139,14 +137,14 @@ impl ResponseError for io::Error {
/// `BadRequest` for `InvalidHeaderValue`
impl ResponseError for header::InvalidHeaderValue {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
/// `BadRequest` for `InvalidHeaderValue`
impl ResponseError for header::InvalidHeaderValueBytes {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
@@ -195,7 +193,7 @@ pub enum ParseError {
/// Return `BadRequest` for `ParseError`
impl ResponseError for ParseError {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
@@ -270,7 +268,7 @@ impl ResponseError for PayloadError {}
/// Return `BadRequest` for `cookie::ParseError`
impl ResponseError for cookie::ParseError {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
@@ -290,8 +288,8 @@ pub enum HttpRangeError {
/// Return `BadRequest` for `HttpRangeError`
impl ResponseError for HttpRangeError {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(
StatusCode::BAD_REQUEST, Body::from("Invalid Range header provided"))
HttpResponse::with_body(
StatusCode::BAD_REQUEST, "Invalid Range header provided")
}
}
@@ -343,7 +341,7 @@ impl From<PayloadError> for MultipartError {
impl ResponseError for MultipartError {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
@@ -360,7 +358,7 @@ pub enum ExpectError {
impl ResponseError for ExpectError {
fn error_response(&self) -> HttpResponse {
HttpExpectationFailed.with_body("Unknown Expect")
HttpResponse::with_body(StatusCode::EXPECTATION_FAILED, "Unknown Expect")
}
}
@@ -378,7 +376,7 @@ pub enum ContentTypeError {
/// Return `BadRequest` for `ContentTypeError`
impl ResponseError for ContentTypeError {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
@@ -410,9 +408,12 @@ impl ResponseError for UrlencodedError {
fn error_response(&self) -> HttpResponse {
match *self {
UrlencodedError::Overflow => httpcodes::HttpPayloadTooLarge.into(),
UrlencodedError::UnknownLength => httpcodes::HttpLengthRequired.into(),
_ => httpcodes::HttpBadRequest.into(),
UrlencodedError::Overflow =>
HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE),
UrlencodedError::UnknownLength =>
HttpResponse::new(StatusCode::LENGTH_REQUIRED),
_ =>
HttpResponse::new(StatusCode::BAD_REQUEST),
}
}
}
@@ -445,8 +446,10 @@ impl ResponseError for JsonPayloadError {
fn error_response(&self) -> HttpResponse {
match *self {
JsonPayloadError::Overflow => httpcodes::HttpPayloadTooLarge.into(),
_ => httpcodes::HttpBadRequest.into(),
JsonPayloadError::Overflow =>
HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE),
_ =>
HttpResponse::new(StatusCode::BAD_REQUEST),
}
}
}
@@ -482,7 +485,7 @@ pub enum UriSegmentError {
impl ResponseError for UriSegmentError {
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
HttpResponse::new(StatusCode::BAD_REQUEST)
}
}
@@ -571,7 +574,7 @@ impl<T> ResponseError for InternalError<T>
where T: Send + Sync + fmt::Debug + 'static
{
fn error_response(&self) -> HttpResponse {
HttpResponse::new(self.status, Body::Empty)
HttpResponse::new(self.status)
}
}