Skip to content

Commit

Permalink
feat: add response::Error
Browse files Browse the repository at this point in the history
This type makes for efficient use of the `?` operator when in a function
with multiple return error types that all implement `IntoResponse`.

Signed-off-by: Nathaniel McCallum <nathaniel@profian.com>
  • Loading branch information
npmccallum committed Apr 8, 2022
1 parent 01a4c88 commit 588086d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
26 changes: 26 additions & 0 deletions axum-core/src/response/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::{IntoResponse, Response};

/// An [IntoResponse]-based 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].
#[derive(Debug)]
pub struct Error(Response);

impl<T: IntoResponse> From<T> for Error {
#[inline]
fn from(value: T) -> Self {
Self(value.into_response())
}
}

impl<T: IntoResponse> IntoResponse for Result<T, Error> {
#[inline]
fn into_response(self) -> Response {
match self {
Ok(ok) => ok.into_response(),
Err(err) => err.0,
}
}
}
3 changes: 3 additions & 0 deletions axum-core/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use crate::body::BoxBody;
mod into_response;
mod into_response_parts;

mod error;

pub use self::{
error::Error,
into_response::IntoResponse,
into_response_parts::{IntoResponseParts, ResponseParts, TryIntoHeaderError},
};
Expand Down

0 comments on commit 588086d

Please sign in to comment.