Refactor: Fix clippy warnings and introduce AppError
This commit is contained in:
1 parent
3add6c3d31
commit
4e1a633dbd
13 files changed
+129
-103
No files matched your search
+17
-1
@@ -6,6 +6,8 @@ use axum::{
|
||||
use serde_json::json;
|
||||
use thiserror::Error;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, AppError>;
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum AppError {
|
||||
#[error("Not Found: {0}")]
|
||||
@@ -19,6 +21,18 @@ pub enum AppError {
|
||||
|
||||
#[error("Bad Request: {0}")]
|
||||
BadRequest(String),
|
||||
|
||||
#[error("Serialization Error: {0}")]
|
||||
JsonError(#[from] serde_json::Error),
|
||||
|
||||
#[error("I/O Error: {0}")]
|
||||
IoError(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
impl From<AppError> for String {
|
||||
fn from(err: AppError) -> String {
|
||||
err.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
@@ -26,8 +40,10 @@ impl IntoResponse for AppError {
|
||||
let (status, error_message) = match &self {
|
||||
AppError::NotFound(msg) => (StatusCode::NOT_FOUND, msg.clone()),
|
||||
AppError::Forbidden(msg) => (StatusCode::FORBIDDEN, msg.clone()),
|
||||
AppError::Internal(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
|
||||
AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg.clone()),
|
||||
AppError::Internal(msg) => (StatusCode::INTERNAL_SERVER_ERROR, msg.clone()),
|
||||
AppError::JsonError(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("JSON Error: {}", e)),
|
||||
AppError::IoError(e) => (StatusCode::INTERNAL_SERVER_ERROR, format!("I/O Error: {}", e)),
|
||||
};
|
||||
|
||||
let body = Json(json!({
|
||||
|
||||
Reference in new issue
Block a user