Compare commits

...

1 Commits

Author SHA1 Message Date
Connor Johnstone
1f36374394 Error handling changes 2026-03-18 15:20:43 -04:00

View File

@@ -15,6 +15,9 @@ pub enum ApiError {
#[error("bad request: {0}")] #[error("bad request: {0}")]
BadRequest(String), BadRequest(String),
#[error("rate limited: {0}")]
TooManyRequests(String),
#[error("internal error: {0}")] #[error("internal error: {0}")]
Internal(String), Internal(String),
} }
@@ -24,6 +27,10 @@ impl ResponseError for ApiError {
let (status, message) = match self { let (status, message) = match self {
ApiError::NotFound(msg) => (actix_web::http::StatusCode::NOT_FOUND, msg.clone()), ApiError::NotFound(msg) => (actix_web::http::StatusCode::NOT_FOUND, msg.clone()),
ApiError::BadRequest(msg) => (actix_web::http::StatusCode::BAD_REQUEST, msg.clone()), ApiError::BadRequest(msg) => (actix_web::http::StatusCode::BAD_REQUEST, msg.clone()),
ApiError::TooManyRequests(msg) => {
tracing::warn!(error = %msg, "rate limited");
(actix_web::http::StatusCode::TOO_MANY_REQUESTS, msg.clone())
}
ApiError::Internal(msg) => { ApiError::Internal(msg) => {
tracing::error!(error = %msg, "internal error"); tracing::error!(error = %msg, "internal error");
( (
@@ -63,6 +70,27 @@ impl From<shanty_search::SearchError> for ApiError {
impl From<shanty_dl::DlError> for ApiError { impl From<shanty_dl::DlError> for ApiError {
fn from(e: shanty_dl::DlError) -> Self { fn from(e: shanty_dl::DlError) -> Self {
match e {
shanty_dl::DlError::RateLimited(msg) => ApiError::TooManyRequests(msg),
other => ApiError::Internal(other.to_string()),
}
}
}
impl From<shanty_index::IndexError> for ApiError {
fn from(e: shanty_index::IndexError) -> Self {
ApiError::Internal(e.to_string())
}
}
impl From<shanty_tag::TagError> for ApiError {
fn from(e: shanty_tag::TagError) -> Self {
ApiError::Internal(e.to_string())
}
}
impl From<shanty_org::OrgError> for ApiError {
fn from(e: shanty_org::OrgError) -> Self {
ApiError::Internal(e.to_string()) ApiError::Internal(e.to_string())
} }
} }