@@ -18,31 +18,88 @@ pub use self::{
1818/// type used with axum.
1919pub type Response < T = BoxBody > = http:: Response < T > ;
2020
21- /// A flexible [ IntoResponse]-based result type
21+ /// An [` IntoResponse` ]-based result type that uses [`ErrorResponse`] as the error type.
2222///
23- /// All types which implement [IntoResponse] can be converted to an [Error].
24- /// This makes it useful as a general error type for functions which combine
25- /// multiple distinct error types but all of which implement [IntoResponse].
23+ /// All types which implement [` IntoResponse` ] can be converted to an [`ErrorResponse`]. This makes
24+ /// it useful as a general purpose error type for functions which combine multiple distinct error
25+ /// types that all implement [` IntoResponse` ].
2626///
27- /// For example, note that the error types below differ. However, both can be
28- /// used with the [Result], and therefore the `?` operator, since they both
29- /// implement [IntoResponse].
27+ /// # Example
3028///
31- /// ```no_run
29+ /// ```
30+ /// use axum::{
31+ /// response::{IntoResponse, Response},
32+ /// http::StatusCode,
33+ /// };
34+ ///
35+ /// // two fallible functions with different error types
36+ /// fn try_something() -> Result<(), ErrorA> {
37+ /// // ...
38+ /// # unimplemented!()
39+ /// }
40+ ///
41+ /// fn try_something_else() -> Result<(), ErrorB> {
42+ /// // ...
43+ /// # unimplemented!()
44+ /// }
45+ ///
46+ /// // each error type implements `IntoResponse`
47+ /// struct ErrorA;
48+ ///
49+ /// impl IntoResponse for ErrorA {
50+ /// fn into_response(self) -> Response {
51+ /// // ...
52+ /// # unimplemented!()
53+ /// }
54+ /// }
55+ ///
56+ /// enum ErrorB {
57+ /// SomethingWentWrong,
58+ /// }
59+ ///
60+ /// impl IntoResponse for ErrorB {
61+ /// fn into_response(self) -> Response {
62+ /// // ...
63+ /// # unimplemented!()
64+ /// }
65+ /// }
66+ ///
67+ /// // we can combine them using `axum::response::Result` and still use `?`
68+ /// async fn handler() -> axum::response::Result<&'static str> {
69+ /// // the errors are automatically converted to `ErrorResponse`
70+ /// try_something()?;
71+ /// try_something_else()?;
72+ ///
73+ /// Ok("it worked!")
74+ /// }
75+ /// ```
76+ ///
77+ /// # As a replacement for `std::result::Result`
78+ ///
79+ /// Since `axum::response::Result` has a default error type you only have to specify the `Ok` type:
80+ ///
81+ /// ```
3282/// use axum::{
33- /// response::{IntoResponse, Response, ResultResponse },
83+ /// response::{IntoResponse, Response, Result },
3484/// http::StatusCode,
3585/// };
3686///
37- /// fn handler() -> ResultResponse<&'static str> {
38- /// Err((StatusCode::NOT_FOUND, "not found"))?;
39- /// Err(StatusCode::BAD_REQUEST)?;
40- /// Ok("ok")
87+ /// // `Result<T>` automatically uses `ErrorResponse` as the error type.
88+ /// async fn handler() -> Result<&'static str> {
89+ /// try_something()?;
90+ ///
91+ /// Ok("it worked!")
92+ /// }
93+ ///
94+ /// // You can still specify the error even if you've imported `axum::response::Result`
95+ /// fn try_something() -> Result<(), StatusCode> {
96+ /// // ...
97+ /// # unimplemented!()
4198/// }
4299/// ```
43- pub type ResultResponse < T > = std:: result:: Result < T , ErrorResponse > ;
100+ pub type Result < T , E = ErrorResponse > = std:: result:: Result < T , E > ;
44101
45- impl < T > IntoResponse for ResultResponse < T >
102+ impl < T > IntoResponse for Result < T >
46103where
47104 T : IntoResponse ,
48105{
54111 }
55112}
56113
57- /// An [IntoResponse]-based error type
114+ /// An [` IntoResponse` ]-based error type
58115///
59- /// See [ResultResponse ] for more details.
116+ /// See [`Result` ] for more details.
60117#[ derive( Debug ) ]
61118pub struct ErrorResponse ( Response ) ;
62119
0 commit comments