From 955598dce9aeb5626654c72b0ef94850123fa8ac Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Sun, 14 Sep 2025 23:27:25 +0300 Subject: Add openapi specs and docs --- src/error/client.rs | 33 +++++++++++++++++++++++++++++++++ src/error/mod.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/error/server.rs | 24 ++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 src/error/client.rs create mode 100644 src/error/mod.rs create mode 100644 src/error/server.rs (limited to 'src/error') diff --git a/src/error/client.rs b/src/error/client.rs new file mode 100644 index 0000000..980e3d2 --- /dev/null +++ b/src/error/client.rs @@ -0,0 +1,33 @@ +pub enum ClientError { + BadJsonBody(String), + BadAuthTokenHeader(String), + UserAlreadyExists { username: String }, + InvalidPassword, + NotAuthorized, +} + +impl ClientError { + pub fn kind(&self) -> String { + match self { + Self::BadJsonBody(..) => "BadJsonBody", + Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader", + Self::UserAlreadyExists { .. } => "UserAlreadyExists", + Self::InvalidPassword => "InvalidPassword", + Self::NotAuthorized => "NotAuthorized", + } + .to_string() + } + + pub fn into_message(self) -> String { + match self { + Self::BadJsonBody(msg) => msg, + Self::BadAuthTokenHeader(msg) => msg, + Self::UserAlreadyExists { username } => { + format!("user with username `{}` already exists", username) + } + Self::InvalidPassword => "password is invalid".to_string(), + + Self::NotAuthorized => "user is not authorized".to_string(), + } + } +} diff --git a/src/error/mod.rs b/src/error/mod.rs new file mode 100644 index 0000000..55d7250 --- /dev/null +++ b/src/error/mod.rs @@ -0,0 +1,50 @@ +mod client; +mod server; + +pub use client::ClientError; +pub use server::ServerError; + +use argon2::password_hash::Error as PasswordHashError; +use axum::extract::rejection::JsonRejection; +use axum_extra::typed_header::TypedHeaderRejection; +use sea_orm::DbErr; + +pub enum ApiError { + Client(ClientError), + Server(ServerError), +} + +impl From for ApiError { + fn from(value: ClientError) -> Self { + Self::Client(value) + } +} +impl From for ApiError { + fn from(value: ServerError) -> Self { + Self::Server(value) + } +} + +impl From for ApiError { + fn from(value: JsonRejection) -> Self { + Self::Client(ClientError::BadJsonBody(value.body_text())) + } +} + +impl From for ApiError { + fn from(value: TypedHeaderRejection) -> Self { + Self::Client(ClientError::BadAuthTokenHeader(value.to_string())) + } +} + +impl From for ApiError { + fn from(value: DbErr) -> Self { + Self::Server(ServerError::Database(value.to_string())) + } +} + +impl From for ApiError { + fn from(value: PasswordHashError) -> Self { + Self::Server(ServerError::PasswordHash(value.to_string())) + } +} diff --git a/src/error/server.rs b/src/error/server.rs new file mode 100644 index 0000000..e67714d --- /dev/null +++ b/src/error/server.rs @@ -0,0 +1,24 @@ +pub enum ServerError { + Database(String), + PasswordHash(String), + Token(String), +} + +impl ServerError { + pub fn kind(&self) -> String { + match self { + Self::Database(..) => "Database", + Self::PasswordHash(..) => "PasswordHash", + Self::Token(..) => "Token", + } + .to_string() + } + + pub fn into_message(self) -> String { + match self { + Self::Database(msg) => msg, + Self::PasswordHash(msg) => msg, + Self::Token(msg) => msg, + } + } +} -- cgit v1.3