feat: better error mappings to http

This commit is contained in:
əlemi 2024-04-23 04:07:32 +02:00
parent 377ed9665d
commit 43d5571b27
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -1,9 +1,11 @@
use axum::http::StatusCode;
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum UpubError { pub enum UpubError {
#[error("database error: {0}")] #[error("database error: {0}")]
Database(#[from] sea_orm::DbErr), Database(#[from] sea_orm::DbErr),
#[error("api returned {0}")] #[error("{0}")]
Status(axum::http::StatusCode), Status(axum::http::StatusCode),
#[error("missing field: {0}")] #[error("missing field: {0}")]
@ -65,7 +67,14 @@ impl axum::response::IntoResponse for UpubError {
let descr = self.to_string(); let descr = self.to_string();
match self { match self {
UpubError::Status(status) => (status, descr).into_response(), UpubError::Status(status) => (status, descr).into_response(),
_ => (axum::http::StatusCode::INTERNAL_SERVER_ERROR, descr).into_response(), UpubError::Database(_) => (StatusCode::SERVICE_UNAVAILABLE, descr).into_response(),
UpubError::Reqwest(x) =>
(
x.status().unwrap_or(StatusCode::INTERNAL_SERVER_ERROR),
format!("failed fetching '{}': {descr}", x.url().map(|x| x.to_string()).unwrap_or_default())
).into_response(),
UpubError::Field(_) => (axum::http::StatusCode::BAD_REQUEST, descr).into_response(),
_ => (StatusCode::INTERNAL_SERVER_ERROR, descr).into_response(),
} }
} }
} }