init
This commit is contained in:
72
ticktalk/crates/backend/src/error.rs
Executable file
72
ticktalk/crates/backend/src/error.rs
Executable file
@@ -0,0 +1,72 @@
|
||||
use actix_web::HttpResponse;
|
||||
use actix_web::error::ResponseError;
|
||||
use serde_json;
|
||||
use thiserror::Error;
|
||||
use ticktalk_db::errors::RepositoryError;
|
||||
use tracing::{error, warn};
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum WebError {
|
||||
#[error("Database error: {0}")]
|
||||
DatabaseError(#[from] RepositoryError),
|
||||
#[error("Invalid UUID: {0}")]
|
||||
InvalidUuid(#[from] uuid::Error),
|
||||
#[error("Not found")]
|
||||
NotFound,
|
||||
#[error("Unauthorized")]
|
||||
Unauthorized,
|
||||
#[error("Bad request")]
|
||||
BadRequest(String),
|
||||
#[error("Internal server error")]
|
||||
InternalServerError(String),
|
||||
}
|
||||
|
||||
impl ResponseError for WebError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
match self {
|
||||
WebError::DatabaseError(_) => {
|
||||
error!("Database error");
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "Database error occurred",
|
||||
"message": "An error occurred while accessing the database"
|
||||
}))
|
||||
}
|
||||
WebError::InvalidUuid(e) => {
|
||||
error!("Invalid UUID error: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "Invalid UUID",
|
||||
"message": "An error occurred while parsing the UUID"
|
||||
}))
|
||||
}
|
||||
WebError::NotFound => {
|
||||
error!("Resource not found");
|
||||
HttpResponse::NotFound().json(serde_json::json!({
|
||||
"error": "Not found",
|
||||
"message": "The requested resource was not found"
|
||||
}))
|
||||
}
|
||||
WebError::Unauthorized => {
|
||||
warn!("Unauthorized access attempt");
|
||||
HttpResponse::Unauthorized().json(serde_json::json!({
|
||||
"error": "Unauthorized",
|
||||
"message": "Access denied"
|
||||
}))
|
||||
}
|
||||
WebError::InternalServerError(e) => {
|
||||
error!("Internal server error: {}", e);
|
||||
HttpResponse::InternalServerError().json(serde_json::json!({
|
||||
"error": "Internal server error",
|
||||
"message": "An unexpected error occurred"
|
||||
}))
|
||||
}
|
||||
|
||||
WebError::BadRequest(msg) => {
|
||||
error!("Bad Request: {}", msg);
|
||||
HttpResponse::BadRequest().json(serde_json::json!({
|
||||
"error": "Bad Request",
|
||||
"message": msg
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user