summaryrefslogtreecommitdiff
path: root/src/error.rs
diff options
context:
space:
mode:
authorbatsumaru <>2026-10-16 11:57:17 +0900
committerbatsumaru <>2026-10-16 11:57:17 +0900
commita69e78b7482227f22e7ddbfaa7dc7ee31f29a99b (patch)
treee829a766484e1937bcbcd4e97668e9d674ea6099 /src/error.rs
parent7dc5038a7438d0eaf63d4bf65f88f6f5ab6b3207 (diff)
Add enum fetchers
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/error.rs b/src/error.rs
new file mode 100644
index 0000000..8b4bb6d
--- /dev/null
+++ b/src/error.rs
@@ -0,0 +1,33 @@
+use axum::{
+ http::StatusCode,
+ response::{IntoResponse, Response},
+};
+
+pub enum AppError {
+ BadRequest,
+ Conflict,
+ Database(sqlx::Error),
+}
+
+impl From<sqlx::Error> for AppError {
+ fn from(e: sqlx::Error) -> Self {
+ match e {
+ sqlx::Error::Database(e) if e.is_unique_violation() => AppError::Conflict,
+ e => AppError::Database(e),
+ }
+ }
+}
+
+impl IntoResponse for AppError {
+ fn into_response(self) -> Response {
+ match self {
+ AppError::BadRequest => StatusCode::BAD_REQUEST,
+ AppError::Conflict => StatusCode::CONFLICT,
+ AppError::Database(e) => {
+ tracing::error!("database error: {e}");
+ StatusCode::INTERNAL_SERVER_ERROR
+ }
+ }
+ .into_response()
+ }
+}