Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IntoResponseParts #797

Merged
merged 22 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions axum-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ mime = "0.3.16"
futures-util = "0.3"
axum = { path = "../axum", version = "0.4" }
hyper = "0.14"
tokio = { version = "1.0", features = ["macros"] }
3 changes: 1 addition & 2 deletions axum-core/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! [`axum::extract`]: https://docs.rs/axum/latest/axum/extract/index.html

use self::rejection::*;
use crate::response::IntoResponse;
use async_trait::async_trait;
use http::{Extensions, HeaderMap, Method, Request, Uri, Version};
use std::convert::Infallible;
Expand Down Expand Up @@ -63,7 +62,7 @@ mod tuple;
pub trait FromRequest<B>: Sized {
/// If the extractor fails it'll use this "rejection" type. A rejection is
/// a kind of error that can be converted into a response.
type Rejection: IntoResponse;
type Rejection: crate::response::IntoResponse;

/// Perform the extraction.
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection>;
Expand Down
13 changes: 5 additions & 8 deletions axum-core/src/extract/rejection.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Rejection response types.

use crate::body;
use http::{Response, StatusCode};
use http_body::Full;
use crate::response::{IntoResponse, Response};
use http::StatusCode;
use std::fmt;

/// Rejection type used if you try and extract the request body more than
Expand All @@ -15,11 +14,9 @@ impl BodyAlreadyExtracted {
const BODY: &'static str = "Cannot have two request body extractors for a single handler";
}

impl crate::response::IntoResponse for BodyAlreadyExtracted {
fn into_response(self) -> crate::response::Response {
let mut res = Response::new(body::boxed(Full::from(Self::BODY)));
*res.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
res
impl IntoResponse for BodyAlreadyExtracted {
fn into_response(self) -> Response {
(StatusCode::INTERNAL_SERVER_ERROR, Self::BODY).into_response()
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
10 changes: 4 additions & 6 deletions axum-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ macro_rules! define_rejection {

impl crate::response::IntoResponse for $name {
fn into_response(self) -> $crate::response::Response {
let body = http_body::Full::from(format!(concat!($body, ": {}"), self.0));
let body = $crate::body::boxed(body);
let mut res =
http::Response::new(body);
*res.status_mut() = http::StatusCode::$status;
res
(
http::StatusCode::$status,
http_body::Full::from(format!(concat!($body, ": {}"), self.0))
).into_response()
}
}

Expand Down
Loading