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 rama_http_types::{
response::{IntoResponse, Response},
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 `rama_http::response::Result` and still use `?`
async fn handler() -> rama_http_types::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 rama_http::response::Result
has a default error type you only have to specify the Ok
type:
use rama_http_types::{
response::{IntoResponse, Response, Result},
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 `rama_http::response::Result`
fn try_something() -> Result<(), StatusCode> {
// ...
}
Aliased Type§
enum Result<T, E = ErrorResponse> {
Ok(T),
Err(E),
}