1
0
Fork 0
forked from alemi/upub

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)]
pub enum UpubError {
#[error("database error: {0}")]
Database(#[from] sea_orm::DbErr),
#[error("api returned {0}")]
#[error("{0}")]
Status(axum::http::StatusCode),
#[error("missing field: {0}")]
@ -65,7 +67,14 @@ impl axum::response::IntoResponse for UpubError {
let descr = self.to_string();
match self {
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(),
}
}
}