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

Fix IntoResponse for tuples overriding error response codes #2468

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
87 changes: 59 additions & 28 deletions axum-core/src/response/into_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use super::{IntoResponseParts, Response, ResponseParts};
use super::{
IntoResponseFailed, IntoResponseParts, OverrideAllStatusCodes, Response, ResponseParts,
};
use crate::{body::Body, BoxError};
use bytes::{buf::Chain, Buf, Bytes, BytesMut};
use http::{
Expand Down Expand Up @@ -329,7 +331,9 @@ where
{
fn into_response(self) -> Response {
let mut res = self.1.into_response();
*res.status_mut() = self.0;
if res.extensions().get::<IntoResponseFailed>().is_none() {
*res.status_mut() = self.0;
}
res
}
}
Expand Down Expand Up @@ -405,18 +409,16 @@ macro_rules! impl_into_response {
let ($($ty),*, res) = self;

let res = res.into_response();
let parts = ResponseParts { res };

$(
let parts = match $ty.into_response_parts(parts) {
if res.extensions().get::<IntoResponseFailed>().is_none() {
let parts = ResponseParts { res };
let parts = match ($($ty,)*).into_response_parts(parts) {
Ok(parts) => parts,
Err(err) => {
return err.into_response();
}
Err(err) => return err.into_response(),
};
)*

parts.res
parts.res
} else {
res
}
}
}

Expand All @@ -430,16 +432,40 @@ macro_rules! impl_into_response {
let (status, $($ty),*, res) = self;

let res = res.into_response();
let parts = ResponseParts { res };

$(
let parts = match $ty.into_response_parts(parts) {
if res.extensions().get::<IntoResponseFailed>().is_none() {
let parts = ResponseParts { res };
let mut parts = match ($($ty,)*).into_response_parts(parts) {
Ok(parts) => parts,
Err(err) => {
return err.into_response();
}
Err(err) => return err.into_response(),
};
)*

// Don't call `(status, parts.res).into_response()` since that checks for
// `IntoResponseFailed` and skips setting the status. We've already done that
// check here so overriding the status is required if returning
// `(IntoResponseFailed, StatusCode::INTERNAL_SERVER_ERROR)`
*parts.res.status_mut() = status;
parts.res
} else {
res
}
}
}

#[allow(non_snake_case)]
impl<R, $($ty,)*> IntoResponse for (OverrideAllStatusCodes, $($ty),*, R)
where
$( $ty: IntoResponseParts, )*
R: IntoResponse,
{
fn into_response(self) -> Response {
let (status, $($ty),*, res) = self;

let res = res.into_response();
let parts = ResponseParts { res };
let parts = match ($($ty,)*).into_response_parts(parts) {
Ok(parts) => parts,
Err(err) => return err.into_response(),
};

(status, parts.res).into_response()
}
Expand All @@ -455,17 +481,22 @@ macro_rules! impl_into_response {
let (outer_parts, $($ty),*, res) = self;

let res = res.into_response();
let parts = ResponseParts { res };
$(
let parts = match $ty.into_response_parts(parts) {
if res.extensions().get::<IntoResponseFailed>().is_none() {
let parts = ResponseParts { res };
let mut parts = match ($($ty,)*).into_response_parts(parts) {
Ok(parts) => parts,
Err(err) => {
return err.into_response();
}
Err(err) => return err.into_response(),
};
)*

(outer_parts, parts.res).into_response()
// Don't call `(outer_parts, parts.res).into_response()` for the same reason we
// don't call `(status, parts.res).into_response()` in the above impl.
*parts.res.status_mut() = outer_parts.status;
parts.res.headers_mut().extend(outer_parts.headers);
parts.res.extensions_mut().extend(outer_parts.extensions);
parts.res
} else {
res
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion axum-core/src/response/into_response_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ macro_rules! impl_into_response_parts {
let res = match $ty.into_response_parts(res) {
Ok(res) => res,
Err(err) => {
return Err(err.into_response());
let mut err_res = err.into_response();
err_res.extensions_mut().insert(super::IntoResponseFailed);
return Err(err_res);
}
};
)*
Expand Down
57 changes: 57 additions & 0 deletions axum-core/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
//!
//! [`axum::response`]: https://docs.rs/axum/0.7/axum/response/index.html

use std::convert::Infallible;

use http::StatusCode;

use crate::body::Body;

mod append_headers;
Expand Down Expand Up @@ -128,3 +132,56 @@ where
Self(value.into_response())
}
}

/// ```
/// todo!();
/// ```
#[derive(Copy, Clone, Debug)]
pub struct IntoResponseFailed;

impl IntoResponseParts for IntoResponseFailed {
type Error = Infallible;

fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
res.extensions_mut().insert(self);
Ok(res)
}
}

/// Not sure it makes sense to return `IntoResponseFailed` as the whole response. You should
/// probably at least combine it with a status code.
///
/// ```compile_fail
/// fn foo()
/// where
/// axum_core::response::IntoResponseFailed: axum_core::response::IntoResponse,
/// {}
/// ```
#[allow(dead_code)]
fn into_response_failed_doesnt_impl_into_response() {}

/// Override all status codes regardless if [`IntoResponseFailed`] is used or not.
///
/// See the docs for [`IntoResponseFailed`] for more details.
#[derive(Debug, Copy, Clone, Default)]
pub struct OverrideAllStatusCodes(pub StatusCode);

impl IntoResponse for OverrideAllStatusCodes {
fn into_response(self) -> Response {
let mut res = ().into_response();
*res.status_mut() = self.0;
res
}
}

impl<R> IntoResponse for (OverrideAllStatusCodes, R)
where
R: IntoResponse,
{
fn into_response(self) -> Response {
let (OverrideAllStatusCodes(status), res) = self;
let mut res = res.into_response();
*res.status_mut() = status;
res
}
}
9 changes: 7 additions & 2 deletions axum-extra/src/protobuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use axum::{
extract::{rejection::BytesRejection, FromRequest, Request},
response::{IntoResponse, Response},
response::{IntoResponse, IntoResponseFailed, Response},
};
use bytes::{Bytes, BytesMut};
use http::StatusCode;
Expand Down Expand Up @@ -122,7 +122,12 @@ where
let mut buf = BytesMut::with_capacity(128);
match &self.0.encode(&mut buf) {
Ok(()) => buf.into_response(),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
IntoResponseFailed,
err.to_string(),
)
.into_response(),
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions axum-extra/src/response/erased_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use axum::{
http::{header, HeaderValue, StatusCode},
response::{IntoResponse, Response},
response::{IntoResponse, IntoResponseFailed, Response},
};
use bytes::{BufMut, Bytes, BytesMut};
use serde::Serialize;
Expand Down Expand Up @@ -77,7 +77,12 @@ impl IntoResponse for ErasedJson {
bytes,
)
.into_response(),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
IntoResponseFailed,
err.to_string(),
)
.into_response(),
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions axum/src/form.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::extract::Request;
use crate::extract::{rejection::*, FromRequest, RawForm};
use axum_core::response::{IntoResponse, Response};
use axum_core::response::{IntoResponse, IntoResponseFailed, Response};
use axum_core::RequestExt;
use http::header::CONTENT_TYPE;
use http::StatusCode;
Expand Down Expand Up @@ -113,7 +113,12 @@ where
body,
)
.into_response(),
Err(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
IntoResponseFailed,
err.to_string(),
)
.into_response(),
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion axum/src/json.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::extract::Request;
use crate::extract::{rejection::*, FromRequest};
use axum_core::response::{IntoResponse, Response};
use axum_core::response::{IntoResponse, IntoResponseFailed, Response};
use bytes::{BufMut, Bytes, BytesMut};
use http::{
header::{self, HeaderMap, HeaderValue},
Expand Down Expand Up @@ -204,6 +204,7 @@ where
header::CONTENT_TYPE,
HeaderValue::from_static(mime::TEXT_PLAIN_UTF_8.as_ref()),
)],
IntoResponseFailed,
err.to_string(),
)
.into_response(),
Expand Down
Loading
Loading