refactor: reorganize ApiError enum for improved clarity and consistency in error handling
This commit is contained in:
parent
9baa24cbc7
commit
9607e55e2a
1 changed files with 46 additions and 24 deletions
|
@ -1,46 +1,68 @@
|
||||||
use std::fmt::{Display, Formatter};
|
|
||||||
use actix_web::{HttpResponse, ResponseError, http::StatusCode};
|
use actix_web::{HttpResponse, ResponseError, http::StatusCode};
|
||||||
use sea_orm::TransactionError;
|
use sea_orm::TransactionError;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
use utoipa::ToSchema;
|
use utoipa::ToSchema;
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
pub enum ApiError {
|
pub enum ApiError {
|
||||||
|
// Database errors
|
||||||
#[error("Database Error: {0}")]
|
#[error("Database Error: {0}")]
|
||||||
Database(#[from] sea_orm::DbErr),
|
Database(#[from] sea_orm::DbErr),
|
||||||
#[error("Unauthorized")]
|
|
||||||
Unauthorized,
|
// Generic HTTP errors
|
||||||
#[error("Not Found")]
|
|
||||||
NotFound,
|
|
||||||
#[error("Bad Request: {0}")]
|
#[error("Bad Request: {0}")]
|
||||||
BadRequest(String),
|
BadRequest(String), // 400 Bad Request
|
||||||
#[error("Validation Error: {0}")]
|
#[error("Unauthorized")]
|
||||||
ValidationError(#[from] validator::ValidationErrors),
|
Unauthorized, // 401 Unauthorized
|
||||||
#[error("Argon2 Error: {0}")]
|
#[error("Not Found")]
|
||||||
Argon2Error(String),
|
NotFound, // 404 Not Found
|
||||||
#[error("Session insert error: {0}")]
|
#[error("Internal Server Error for endpoint: {0}")]
|
||||||
SessionInsertError(#[from] actix_session::SessionInsertError),
|
InternalServerError(String), // 500 Internal Server Error
|
||||||
|
|
||||||
|
// Session errors
|
||||||
#[error("Already logged in")]
|
#[error("Already logged in")]
|
||||||
AlreadyLoggedIn,
|
AlreadyLoggedIn,
|
||||||
|
#[error("Session insert error: {0}")]
|
||||||
|
SessionInsertError(#[from] actix_session::SessionInsertError),
|
||||||
|
|
||||||
|
// Validation errors
|
||||||
|
#[error("Validation Error: {0}")]
|
||||||
|
ValidationError(#[from] validator::ValidationErrors),
|
||||||
|
|
||||||
|
// Argon2 errors
|
||||||
|
#[error("Argon2 Error: {0}")]
|
||||||
|
Argon2Error(String),
|
||||||
|
|
||||||
|
// User errors
|
||||||
#[error("User with username - {0} - already exists")]
|
#[error("User with username - {0} - already exists")]
|
||||||
UserAlreadyExists(String),
|
UserAlreadyExists(String),
|
||||||
#[error("Internal Server Error for endpoint: {0}")]
|
|
||||||
InternalServerError(String)
|
|
||||||
}
|
}
|
||||||
impl ResponseError for ApiError {
|
impl ResponseError for ApiError {
|
||||||
fn status_code(&self) -> StatusCode {
|
fn status_code(&self) -> StatusCode {
|
||||||
match self {
|
match self {
|
||||||
ApiError::Database(..) => StatusCode::INTERNAL_SERVER_ERROR,
|
// Database errors
|
||||||
ApiError::NotFound => StatusCode::NOT_FOUND,
|
ApiError::Database(..) => StatusCode::INTERNAL_SERVER_ERROR, // 500 Internal Server Error
|
||||||
ApiError::Unauthorized => StatusCode::UNAUTHORIZED,
|
|
||||||
ApiError::BadRequest(..) => StatusCode::BAD_REQUEST,
|
// Generic HTTP errors
|
||||||
ApiError::ValidationError(..) => StatusCode::BAD_REQUEST,
|
ApiError::BadRequest(..) => StatusCode::BAD_REQUEST, // 400 Bad Request
|
||||||
ApiError::Argon2Error(..) => StatusCode::INTERNAL_SERVER_ERROR,
|
ApiError::Unauthorized => StatusCode::UNAUTHORIZED, // 401 Unauthorized
|
||||||
ApiError::SessionInsertError(..) => StatusCode::INTERNAL_SERVER_ERROR,
|
ApiError::NotFound => StatusCode::NOT_FOUND, // 404 Not Found
|
||||||
ApiError::AlreadyLoggedIn => StatusCode::CONFLICT,
|
ApiError::InternalServerError(..) => StatusCode::INTERNAL_SERVER_ERROR, // 500 Internal Server Error
|
||||||
ApiError::UserAlreadyExists(..) => StatusCode::CONFLICT,
|
|
||||||
ApiError::InternalServerError(..) => StatusCode::INTERNAL_SERVER_ERROR,
|
// Session errors
|
||||||
|
ApiError::AlreadyLoggedIn => StatusCode::CONFLICT, // 409 Conflict
|
||||||
|
ApiError::SessionInsertError(..) => StatusCode::INTERNAL_SERVER_ERROR, // 500 Internal Server Error
|
||||||
|
|
||||||
|
// Validation errors
|
||||||
|
ApiError::ValidationError(..) => StatusCode::BAD_REQUEST, // 400 Bad Request
|
||||||
|
|
||||||
|
// Argon2 errors
|
||||||
|
ApiError::Argon2Error(..) => StatusCode::INTERNAL_SERVER_ERROR, // 500 Internal Server Error
|
||||||
|
|
||||||
|
// User errors
|
||||||
|
ApiError::UserAlreadyExists(..) => StatusCode::CONFLICT, // 409 Conflict
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue