Skip to content

Commit

Permalink
nitpicky docs changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpdrsn committed Apr 21, 2022
1 parent 8b324c7 commit fa0e886
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 18 deletions.
91 changes: 74 additions & 17 deletions axum-core/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,88 @@ pub use self::{
/// type used with axum.
pub type Response<T = BoxBody> = http::Response<T>;

/// A flexible [IntoResponse]-based result type
/// An [`IntoResponse`]-based result type that uses [`ErrorResponse`] as the error type.
///
/// All types which implement [IntoResponse] can be converted to an [Error].
/// This makes it useful as a general error type for functions which combine
/// multiple distinct error types but all of which implement [IntoResponse].
/// All types which implement [`IntoResponse`] can be converted to an [`ErrorResponse`]. This makes
/// it useful as a general purpose error type for functions which combine multiple distinct error
/// types that all implement [`IntoResponse`].
///
/// For example, note that the error types below differ. However, both can be
/// used with the [Result], and therefore the `?` operator, since they both
/// implement [IntoResponse].
/// # Example
///
/// ```no_run
/// ```
/// use axum::{
/// response::{IntoResponse, Response},
/// http::StatusCode,
/// };
///
/// // two fallible functions with different error types
/// fn try_something() -> Result<(), ErrorA> {
/// // ...
/// # unimplemented!()
/// }
///
/// fn try_something_else() -> Result<(), ErrorB> {
/// // ...
/// # unimplemented!()
/// }
///
/// // each error type implements `IntoResponse`
/// struct ErrorA;
///
/// impl IntoResponse for ErrorA {
/// fn into_response(self) -> Response {
/// // ...
/// # unimplemented!()
/// }
/// }
///
/// enum ErrorB {
/// SomethingWentWrong,
/// }
///
/// impl IntoResponse for ErrorB {
/// fn into_response(self) -> Response {
/// // ...
/// # unimplemented!()
/// }
/// }
///
/// // we can combine them using `axum::response::Result` and still use `?`
/// async fn handler() -> axum::response::Result<&'static str> {
/// // the errors are automatically converted to `ErrorResponse`
/// try_something()?;
/// try_something_else()?;
///
/// Ok("it worked!")
/// }
/// ```
///
/// # As a replacement for `std::result::Result`
///
/// Since `axum::response::Result` has a default error type you only have to specify the `Ok` type:
///
/// ```
/// use axum::{
/// response::{IntoResponse, Response, ResultResponse},
/// response::{IntoResponse, Response, Result},
/// http::StatusCode,
/// };
///
/// fn handler() -> ResultResponse<&'static str> {
/// Err((StatusCode::NOT_FOUND, "not found"))?;
/// Err(StatusCode::BAD_REQUEST)?;
/// Ok("ok")
/// // `Result<T>` automatically uses `ErrorResponse` as the error type.
/// async fn handler() -> Result<&'static str> {
/// try_something()?;
///
/// Ok("it worked!")
/// }
///
/// // You can still specify the error even if you've imported `axum::response::Result`
/// fn try_something() -> Result<(), StatusCode> {
/// // ...
/// # unimplemented!()
/// }
/// ```
pub type ResultResponse<T> = std::result::Result<T, ErrorResponse>;
pub type Result<T, E = ErrorResponse> = std::result::Result<T, E>;

impl<T> IntoResponse for ResultResponse<T>
impl<T> IntoResponse for Result<T>
where
T: IntoResponse,
{
Expand All @@ -54,9 +111,9 @@ where
}
}

/// An [IntoResponse]-based error type
/// An [`IntoResponse`]-based error type
///
/// See [ResultResponse] for more details.
/// See [`Result`] for more details.
#[derive(Debug)]
pub struct ErrorResponse(Response);

Expand Down
2 changes: 1 addition & 1 deletion axum/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub use crate::Extension;

#[doc(inline)]
pub use axum_core::response::{
ErrorResponse, IntoResponse, IntoResponseParts, Response, ResponseParts, ResultResponse,
ErrorResponse, IntoResponse, IntoResponseParts, Response, ResponseParts, Result,
};

#[doc(inline)]
Expand Down

0 comments on commit fa0e886

Please sign in to comment.