Skip to content

Commit fa0e886

Browse files
committed
nitpicky docs changes
1 parent 8b324c7 commit fa0e886

2 files changed

Lines changed: 75 additions & 18 deletions

File tree

axum-core/src/response/mod.rs

Lines changed: 74 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,88 @@ pub use self::{
1818
/// type used with axum.
1919
pub 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>
46103
where
47104
T: IntoResponse,
48105
{
@@ -54,9 +111,9 @@ where
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)]
61118
pub struct ErrorResponse(Response);
62119

axum/src/response/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use crate::Extension;
2020

2121
#[doc(inline)]
2222
pub use axum_core::response::{
23-
ErrorResponse, IntoResponse, IntoResponseParts, Response, ResponseParts, ResultResponse,
23+
ErrorResponse, IntoResponse, IntoResponseParts, Response, ResponseParts, Result,
2424
};
2525

2626
#[doc(inline)]

0 commit comments

Comments
 (0)