diff options
Diffstat (limited to 'src/routes/errors.rs')
| -rw-r--r-- | src/routes/errors.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/src/routes/errors.rs b/src/routes/errors.rs new file mode 100644 index 0000000..6261d75 --- /dev/null +++ b/src/routes/errors.rs @@ -0,0 +1,26 @@ +use axum::{Json, body::Body, http::{Response, StatusCode}, response::IntoResponse}; +use serde_json::{json}; + +pub enum RouteError { + Internal(String), + UserCreateEmailExists(String), + UserCreateUsernameExists(String), +} + +impl IntoResponse for RouteError { + fn into_response(self) -> Response<Body> { + let (status, message) = match self { + Self::Internal(message) => { + (StatusCode::INTERNAL_SERVER_ERROR, message) + }, + RouteError::UserCreateEmailExists(email) => { + (StatusCode::BAD_REQUEST, format!("user with email \"{}\" already exists", email)) + }, + RouteError::UserCreateUsernameExists(username) => { + (StatusCode::BAD_REQUEST, format!("user with username \"{}\" already exists", username)) + } + }; + + (status, Json(json!({"error": message}))).into_response() + } +} |
