pub type Result<T, E = ErrorResponse> = Result<T, E>;
Expand description
An IntoResponse
-based result type that uses ErrorResponse
as the error type.
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
.
§Example
use axum::{
response::{IntoResponse, Response},
http::StatusCode,
};
// two fallible functions with different error types
fn try_something() -> Result<(), ErrorA> {
// ...
}
fn try_something_else() -> Result<(), ErrorB> {
// ...
}
// each error type implements `IntoResponse`
struct ErrorA;
impl IntoResponse for ErrorA {
fn into_response(self) -> Response {
// ...
}
}
enum ErrorB {
SomethingWentWrong,
}
impl IntoResponse for ErrorB {
fn into_response(self) -> Response {
// ...
}
}
// 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, Result},
http::StatusCode,
};
// `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> {
// ...
}
Aliased Type§
enum Result<T, E = ErrorResponse> {
Ok(T),
Err(E),
}
Variants§
Trait Implementations§
Source§impl<T> IntoResponse for Result<T>where
T: IntoResponse,
impl<T> IntoResponse for Result<T>where
T: IntoResponse,
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Create a response.
Layout§
Note: Unable to compute type layout, possibly due to this type having generic parameters. Layout can only be computed for concrete, fully-instantiated types.