aboutsummaryrefslogtreecommitdiffstats
path: root/src/server/error/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/error/mod.rs')
-rw-r--r--src/server/error/mod.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/server/error/mod.rs b/src/server/error/mod.rs
new file mode 100644
index 0000000..6d07f9f
--- /dev/null
+++ b/src/server/error/mod.rs
@@ -0,0 +1,27 @@
+use axum::{
+ http::StatusCode,
+ response::{IntoResponse, Response},
+};
+
+#[derive(Debug)]
+pub struct AppError(anyhow::Error);
+
+// Tell axum how to convert `AppError` into a response.
+impl IntoResponse for AppError {
+ fn into_response(self) -> Response {
+ tracing::error!("Application error: {:#}", self.0);
+
+ (StatusCode::INTERNAL_SERVER_ERROR, "Something went wrong").into_response()
+ }
+}
+
+// This enables using `?` on functions that return `Result<_, anyhow::Error>` to turn them into
+// `Result<_, AppError>`. That way you don't need to do that manually.
+impl<E> From<E> for AppError
+where
+ E: Into<anyhow::Error>,
+{
+ fn from(err: E) -> Self {
+ Self(err.into())
+ }
+}