35 lines
1.0 KiB
Rust
35 lines
1.0 KiB
Rust
use axum::{
|
|
http::StatusCode,
|
|
response::{IntoResponse, Response},
|
|
};
|
|
use sqlx::Error as SqlxError;
|
|
use thiserror::Error;
|
|
|
|
#[derive(Error, Debug)]
|
|
pub(crate) enum AppError {
|
|
#[error("Account not found: {0}")]
|
|
AccountNotFound(i64),
|
|
#[error("Account already exists")]
|
|
AccountAlreadyExists,
|
|
#[error("Insufficient funds")]
|
|
InsufficientFunds,
|
|
#[error("Database error: {0}")]
|
|
DatabaseError(#[from] SqlxError),
|
|
#[error("Invalid input: {0}")]
|
|
InvalidInput(String),
|
|
}
|
|
|
|
impl IntoResponse for AppError {
|
|
fn into_response(self) -> Response {
|
|
let status = match self {
|
|
AppError::AccountNotFound(_) => StatusCode::NOT_FOUND,
|
|
AppError::InsufficientFunds => StatusCode::BAD_REQUEST,
|
|
AppError::AccountAlreadyExists => StatusCode::BAD_REQUEST,
|
|
AppError::DatabaseError(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
|
AppError::InvalidInput(_) => StatusCode::BAD_REQUEST,
|
|
};
|
|
|
|
(status, self.to_string()).into_response()
|
|
}
|
|
}
|