#![cfg_attr(coverage_nightly, coverage(off))]
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
#[derive(Debug)]
pub(super) enum AppError {
BadRequest(String),
Internal(anyhow::Error),
}
impl From<anyhow::Error> for AppError {
fn from(err: anyhow::Error) -> Self {
let err_str = err.to_string();
if err_str.contains("Path not found")
|| err_str.contains("Invalid timeout")
|| err_str.contains("Too many files")
|| err_str.contains("Invalid parameter")
|| err_str.contains("Missing required")
|| err_str.contains("must be")
{
AppError::BadRequest(err_str)
} else {
AppError::Internal(err)
}
}
}
impl IntoResponse for AppError {
fn into_response(self) -> Response {
let (status, message) = match self {
AppError::BadRequest(msg) => (StatusCode::BAD_REQUEST, msg),
AppError::Internal(err) => (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()),
};
let body = Json(json!({
"error": message
}));
(status, body).into_response()
}
}