This commit is contained in:
34
src/errors.rs
Normal file
34
src/errors.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
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()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user