summaryrefslogtreecommitdiffstats
path: root/src/error.rs
diff options
context:
space:
mode:
authorToby Vincent <tobyv@tobyvin.dev>2024-03-21 21:39:15 -0500
committerToby Vincent <tobyv@tobyvin.dev>2024-03-21 21:39:15 -0500
commitfd1447999d9665866d65002b2c2317b8b150225f (patch)
treedce0c5282a60fc27f65a066200fcd8aa86b4370e /src/error.rs
parentf977dce01be9de61a64b94aab883fb43949234b3 (diff)
feat: impl user api endpoint
Diffstat (limited to 'src/error.rs')
-rw-r--r--src/error.rs48
1 files changed, 34 insertions, 14 deletions
diff --git a/src/error.rs b/src/error.rs
index 1f4b354..a5b48ff 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -1,26 +1,46 @@
-use axum::{
- http::StatusCode,
- response::{IntoResponse, Response},
- Json,
-};
-
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
- #[error("IO error: {0}")]
+ #[error(transparent)]
IO(#[from] std::io::Error),
- #[error("Axum Error: {0:?}")]
+ #[error(transparent)]
+ TaskJoin(#[from] tokio::task::JoinError),
+
+ #[error(transparent)]
Axum(#[from] axum::Error),
+
+ #[error(transparent)]
+ Sqlx(#[from] sqlx::Error),
+
+ #[error(transparent)]
+ Migration(#[from] sqlx::migrate::MigrateError),
+
+ #[error("User not found: {0}")]
+ UserNotFound(uuid::Uuid),
}
-impl IntoResponse for Error {
- fn into_response(self) -> Response {
- let body = Json(serde_json::json!({
- "error": self.to_string(),
- }));
+impl axum::response::IntoResponse for Error {
+ fn into_response(self) -> axum::response::Response {
+ use axum::{http::StatusCode, Json};
+ use serde_json::json;
- (StatusCode::INTERNAL_SERVER_ERROR, body).into_response()
+ match self {
+ Error::UserNotFound(uuid) => (
+ StatusCode::BAD_REQUEST,
+ Json(json!({
+ "status": "fail",
+ "message": uuid,
+ })),
+ ),
+ err => (
+ StatusCode::INTERNAL_SERVER_ERROR,
+ Json(json!({
+ "error": err.to_string(),
+ })),
+ ),
+ }
+ .into_response()
}
}