Skip to content

Commit 723aa0a

Browse files
risssondavepgreene
authored andcommitted
packages/ak-axum/error: init (goauthentik#21315)
1 parent e927074 commit 723aa0a

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/ak-axum/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ license-file.workspace = true
1010
publish.workspace = true
1111

1212
[dependencies]
13+
ak-common.workspace = true
1314
axum.workspace = true
1415
durstr.workspace = true
15-
ak-common.workspace = true
16-
tower.workspace = true
16+
eyre.workspace = true
1717
tower-http.workspace = true
18+
tower.workspace = true
19+
tracing.workspace = true
1820

1921
[lints]
2022
workspace = true

packages/ak-axum/src/error.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Custom error type for use in [`axum`] handlers.
2+
3+
use axum::{
4+
http::StatusCode,
5+
response::{IntoResponse, Response},
6+
};
7+
use eyre::Report;
8+
use tracing::warn;
9+
10+
/// Custom error type for use in [`axum`] handlers, wrapping a [`Report`].
11+
///
12+
/// It implements [`IntoResponse`] and logs errors before returning a 502.
13+
#[derive(Debug)]
14+
pub struct AppError(pub Report);
15+
16+
impl<E> From<E> for AppError
17+
where
18+
E: Into<Report>,
19+
{
20+
fn from(err: E) -> Self {
21+
Self(err.into())
22+
}
23+
}
24+
25+
impl IntoResponse for AppError {
26+
fn into_response(self) -> Response {
27+
warn!(error = ?self.0, "error occurred");
28+
(StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong").into_response()
29+
}
30+
}
31+
32+
/// Result type with [`AppError`].
33+
pub type Result<T, E = AppError> = std::result::Result<T, E>;

packages/ak-axum/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
//! Utilities for working with [`axum`].
22
3+
pub mod error;
34
pub mod router;

0 commit comments

Comments
 (0)