Skip to content

Commit

Permalink
add kitsune-error crate
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Apr 6, 2024
1 parent 6b2fa3a commit c588950
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
"crates/kitsune-db",
"crates/kitsune-email",
"crates/kitsune-embed",
"crates/kitsune-error",
"crates/kitsune-federation",
"crates/kitsune-federation-filter",
"crates/kitsune-http-client",
Expand Down
15 changes: 15 additions & 0 deletions crates/kitsune-error/Cargo.toml
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
27 changes: 27 additions & 0 deletions crates/kitsune-error/src/axum.rs
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(),
)
}

Check warning on line 14 in crates/kitsune-error/src/axum.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/axum.rs#L6-L14

Added lines #L6 - L14 were not covered by tests

impl IntoResponse for Error {
fn into_response(self) -> Response {
debug!(error = ?self.inner);

Check warning on line 18 in crates/kitsune-error/src/axum.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/axum.rs#L17-L18

Added lines #L17 - L18 were not covered by tests

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(),

Check warning on line 24 in crates/kitsune-error/src/axum.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/axum.rs#L20-L24

Added lines #L20 - L24 were not covered by tests
}
}

Check warning on line 26 in crates/kitsune-error/src/axum.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/axum.rs#L26

Added line #L26 was not covered by tests
}
24 changes: 24 additions & 0 deletions crates/kitsune-error/src/ext.rs
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(),
})
}

Check warning on line 23 in crates/kitsune-error/src/ext.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/ext.rs#L18-L23

Added lines #L18 - L23 were not covered by tests
}
58 changes: 58 additions & 0 deletions crates/kitsune-error/src/lib.rs
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(),
}
}

Check warning on line 31 in crates/kitsune-error/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/lib.rs#L23-L31

Added lines #L23 - L31 were not covered by tests

#[must_use]
pub fn error_type(&self) -> &ErrorType {
&self.ty
}

Check warning on line 36 in crates/kitsune-error/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/lib.rs#L34-L36

Added lines #L34 - L36 were not covered by tests

pub fn error(&self) -> &eyre::Report {
&self.inner
}

Check warning on line 40 in crates/kitsune-error/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/lib.rs#L38-L40

Added lines #L38 - L40 were not covered by tests

#[must_use]
pub fn with_error_type(self, ty: ErrorType) -> Self {
Self { ty, ..self }
}

Check warning on line 45 in crates/kitsune-error/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/lib.rs#L43-L45

Added lines #L43 - L45 were not covered by tests
}

impl<T> From<T> for Error
where
T: Into<eyre::Report>,
{
fn from(value: T) -> Self {
Self {
ty: ErrorType::Other,
inner: value.into(),
}
}

Check warning on line 57 in crates/kitsune-error/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/kitsune-error/src/lib.rs#L52-L57

Added lines #L52 - L57 were not covered by tests
}

0 comments on commit c588950

Please sign in to comment.