-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "kitsune-error" | ||
authors.workspace = true | ||
edition.workspace = true | ||
version.workspace = true | ||
license.workspace = true | ||
|
||
[dependencies] | ||
axum-core = "0.4.3" | ||
eyre = "0.6.12" | ||
http = "1.1.0" | ||
tracing = "0.1.40" | ||
|
||
[lints] | ||
workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use crate::{Error, ErrorType}; | ||
use axum_core::response::{IntoResponse, Response}; | ||
use http::StatusCode; | ||
|
||
#[inline] | ||
fn to_response<B>(status_code: StatusCode, maybe_body: Option<B>) -> Response | ||
where | ||
B: IntoResponse, | ||
{ | ||
maybe_body.map_or_else( | ||
|| status_code.into_response(), | ||
|body| (status_code, body).into_response(), | ||
) | ||
} | ||
|
||
impl IntoResponse for Error { | ||
fn into_response(self) -> Response { | ||
debug!(error = ?self.inner); | ||
|
||
match self.ty { | ||
ErrorType::BadRequest(maybe_body) => to_response(StatusCode::BAD_REQUEST, maybe_body), | ||
ErrorType::NotFound => StatusCode::NOT_FOUND.into_response(), | ||
ErrorType::Unauthorized => StatusCode::UNAUTHORIZED.into_response(), | ||
ErrorType::Other => StatusCode::INTERNAL_SERVER_ERROR.into_response(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use crate::{Error, ErrorType}; | ||
|
||
mod sealed { | ||
pub trait Sealed {} | ||
|
||
impl<T, E> Sealed for Result<T, E> {} | ||
} | ||
|
||
pub trait ResultExt<T>: sealed::Sealed { | ||
fn with_error_type(self, ty: ErrorType) -> Result<T, Error>; | ||
} | ||
|
||
impl<T, E> ResultExt<T> for Result<T, E> | ||
where | ||
E: Into<eyre::Report>, | ||
{ | ||
#[inline] | ||
fn with_error_type(self, ty: ErrorType) -> Result<T, Error> { | ||
self.map_err(|err| Error { | ||
ty, | ||
inner: err.into(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#[macro_use] | ||
extern crate tracing; | ||
|
||
pub use self::ext::ResultExt; | ||
|
||
mod axum; | ||
mod ext; | ||
|
||
#[derive(Clone)] | ||
pub enum ErrorType { | ||
BadRequest(Option<String>), | ||
NotFound, | ||
Unauthorized, | ||
Other, | ||
} | ||
|
||
pub struct Error { | ||
ty: ErrorType, | ||
inner: eyre::Report, | ||
} | ||
|
||
impl Error { | ||
pub fn new<E>(ty: ErrorType, err: E) -> Self | ||
where | ||
E: Into<eyre::Report>, | ||
{ | ||
Self { | ||
ty, | ||
inner: err.into(), | ||
} | ||
} | ||
|
||
#[must_use] | ||
pub fn error_type(&self) -> &ErrorType { | ||
&self.ty | ||
} | ||
|
||
pub fn error(&self) -> &eyre::Report { | ||
&self.inner | ||
} | ||
|
||
#[must_use] | ||
pub fn with_error_type(self, ty: ErrorType) -> Self { | ||
Self { ty, ..self } | ||
} | ||
} | ||
|
||
impl<T> From<T> for Error | ||
where | ||
T: Into<eyre::Report>, | ||
{ | ||
fn from(value: T) -> Self { | ||
Self { | ||
ty: ErrorType::Other, | ||
inner: value.into(), | ||
} | ||
} | ||
} |