[update] improved error handling in handlers.rs
by differentiating database errors, providing clearer response messages, and adding error logging with tracing
This commit is contained in:
@@ -17,16 +17,19 @@ use axum::{
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use sqlx::error::DatabaseError;
|
||||
|
||||
pub struct AppError(anyhow::Error);
|
||||
|
||||
impl IntoResponse for AppError {
|
||||
fn into_response(self) -> Response {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Something went wrong: {}", self.0),
|
||||
)
|
||||
.into_response()
|
||||
let (status, message) = match self.0.downcast_ref::<sqlx::Error>() {
|
||||
Some(_) => (StatusCode::INTERNAL_SERVER_ERROR, "Database error occurred"),
|
||||
None => (StatusCode::INTERNAL_SERVER_ERROR, "An error occurred"),
|
||||
};
|
||||
|
||||
tracing::error!("API Error: {:?}", self.0);
|
||||
(status, message).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user