axum_core/response/mod.rs
1//! Types and traits for generating responses.
2//!
3//! See [`axum::response`] for more details.
4//!
5//! [`axum::response`]: https://docs.rs/axum/0.7/axum/response/index.html
6
7use crate::body::Body;
8
9mod append_headers;
10mod into_response;
11mod into_response_parts;
12
13pub use self::{
14 append_headers::AppendHeaders,
15 into_response::IntoResponse,
16 into_response_parts::{IntoResponseParts, ResponseParts, TryIntoHeaderError},
17};
18
19/// Type alias for [`http::Response`] whose body type defaults to [`Body`], the most common body
20/// type used with axum.
21pub type Response<T = Body> = http::Response<T>;
22
23/// An [`IntoResponse`]-based result type that uses [`ErrorResponse`] as the error type.
24///
25/// All types which implement [`IntoResponse`] can be converted to an [`ErrorResponse`]. This makes
26/// it useful as a general purpose error type for functions which combine multiple distinct error
27/// types that all implement [`IntoResponse`].
28///
29/// # Example
30///
31/// ```
32/// use axum::{
33/// response::{IntoResponse, Response},
34/// http::StatusCode,
35/// };
36///
37/// // two fallible functions with different error types
38/// fn try_something() -> Result<(), ErrorA> {
39/// // ...
40/// # unimplemented!()
41/// }
42///
43/// fn try_something_else() -> Result<(), ErrorB> {
44/// // ...
45/// # unimplemented!()
46/// }
47///
48/// // each error type implements `IntoResponse`
49/// struct ErrorA;
50///
51/// impl IntoResponse for ErrorA {
52/// fn into_response(self) -> Response {
53/// // ...
54/// # unimplemented!()
55/// }
56/// }
57///
58/// enum ErrorB {
59/// SomethingWentWrong,
60/// }
61///
62/// impl IntoResponse for ErrorB {
63/// fn into_response(self) -> Response {
64/// // ...
65/// # unimplemented!()
66/// }
67/// }
68///
69/// // we can combine them using `axum::response::Result` and still use `?`
70/// async fn handler() -> axum::response::Result<&'static str> {
71/// // the errors are automatically converted to `ErrorResponse`
72/// try_something()?;
73/// try_something_else()?;
74///
75/// Ok("it worked!")
76/// }
77/// ```
78///
79/// # As a replacement for `std::result::Result`
80///
81/// Since `axum::response::Result` has a default error type you only have to specify the `Ok` type:
82///
83/// ```
84/// use axum::{
85/// response::{IntoResponse, Response, Result},
86/// http::StatusCode,
87/// };
88///
89/// // `Result<T>` automatically uses `ErrorResponse` as the error type.
90/// async fn handler() -> Result<&'static str> {
91/// try_something()?;
92///
93/// Ok("it worked!")
94/// }
95///
96/// // You can still specify the error even if you've imported `axum::response::Result`
97/// fn try_something() -> Result<(), StatusCode> {
98/// // ...
99/// # unimplemented!()
100/// }
101/// ```
102pub type Result<T, E = ErrorResponse> = std::result::Result<T, E>;
103
104impl<T> IntoResponse for Result<T>
105where
106 T: IntoResponse,
107{
108 fn into_response(self) -> Response {
109 match self {
110 Ok(ok) => ok.into_response(),
111 Err(err) => err.0,
112 }
113 }
114}
115
116/// An [`IntoResponse`]-based error type
117///
118/// See [`Result`] for more details.
119#[derive(Debug)]
120#[must_use]
121pub struct ErrorResponse(Response);
122
123impl<T> From<T> for ErrorResponse
124where
125 T: IntoResponse,
126{
127 fn from(value: T) -> Self {
128 Self(value.into_response())
129 }
130}