From 67e5ecee5107179d7aa885a737ce521aef755a6b Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Thu, 25 Sep 2025 01:21:50 +0300 Subject: Improve the errors --- src/error/client.rs | 8 ++++---- src/routers/account.rs | 36 ++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/src/error/client.rs b/src/error/client.rs index 70b6001..302581e 100644 --- a/src/error/client.rs +++ b/src/error/client.rs @@ -1,7 +1,7 @@ pub enum ClientError { BadJsonBody(String), BadAuthTokenHeader(String), - UserAlreadyExists { username: String }, + UsernameIsTaken { username: String }, InvalidPassword, NotAuthorized, UserNotFound { id: i64 }, @@ -13,7 +13,7 @@ impl ClientError { match self { Self::BadJsonBody(..) => "BadJsonBody", Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader", - Self::UserAlreadyExists { .. } => "UserAlreadyExists", + Self::UsernameIsTaken { .. } => "UsernameIsTaken", Self::InvalidPassword => "InvalidPassword", Self::NotAuthorized => "NotAuthorized", Self::UserNotFound { .. } => "UserNotFound", @@ -26,8 +26,8 @@ impl ClientError { match self { Self::BadJsonBody(msg) => msg, Self::BadAuthTokenHeader(msg) => msg, - Self::UserAlreadyExists { username } => { - format!("user with username `{}` already exists", username) + Self::UsernameIsTaken { username } => { + format!("username `{}` is taken", username) } Self::InvalidPassword => format!("password is invalid"), diff --git a/src/routers/account.rs b/src/routers/account.rs index a6fce99..71ba496 100644 --- a/src/routers/account.rs +++ b/src/routers/account.rs @@ -2,22 +2,30 @@ use axum::extract::State; use chrono::{DateTime, Duration, Utc}; use entity::users::{self}; use sea_orm::{ - ActiveModelTrait, ActiveValue::Set, ColumnTrait, EntityTrait, IntoActiveModel, ModelTrait, - QueryFilter, + ActiveModelTrait, ActiveValue::Set, ColumnTrait, DatabaseConnection, EntityTrait, + IntoActiveModel, ModelTrait, QueryFilter, }; use serde::{Deserialize, Serialize}; use utoipa::ToSchema; use utoipa_axum::{router::OpenApiRouter, routes}; use crate::{ - ApiResult, AppState, ClientError, GlobalResponses, JwtClaims, ServerError, SuccessResponse, - create_jwt, create_password, + ApiError, ApiResult, AppState, ClientError, GlobalResponses, JwtClaims, ServerError, + SuccessResponse, create_jwt, create_password, extract::{ApiJson, Auth}, models::Account, tags::ACCOUNT, validate_password, }; +async fn username_exists(username: &str, db: &DatabaseConnection) -> Result { + Ok(users::Entity::find() + .filter(users::Column::Username.eq(username)) + .one(db) + .await? + .is_some()) +} + #[derive(Serialize, ToSchema)] #[schema(description = "Authorization token information")] struct Token { @@ -130,14 +138,8 @@ async fn register( State(state): State, ApiJson(req): ApiJson, ) -> ApiResult { - let user_exists = users::Entity::find() - .filter(users::Column::Username.eq(&req.username)) - .one(&state.db) - .await? - .is_some(); - - if user_exists { - return Err(ClientError::UserAlreadyExists { + if username_exists(&req.username, &state.db).await? { + return Err(ClientError::UsernameIsTaken { username: req.username, } .into()); @@ -255,14 +257,8 @@ async fn change_username( Auth(user): Auth, ApiJson(req): ApiJson, ) -> ApiResult { - let user_exists = users::Entity::find() - .filter(users::Column::Username.eq(&req.new_username)) - .one(&state.db) - .await? - .is_some(); - - if user_exists { - return Err(ClientError::UserAlreadyExists { + if username_exists(&req.new_username, &state.db).await? { + return Err(ClientError::UsernameIsTaken { username: req.new_username, } .into()); -- cgit v1.2.3