diff --git a/src/error.rs b/src/error.rs index 99c47b8..688f731 100644 --- a/src/error.rs +++ b/src/error.rs @@ -15,6 +15,9 @@ pub enum ApiError { #[error("bad request: {0}")] BadRequest(String), + #[error("rate limited: {0}")] + TooManyRequests(String), + #[error("internal error: {0}")] Internal(String), } @@ -24,6 +27,10 @@ impl ResponseError for ApiError { let (status, message) = match self { ApiError::NotFound(msg) => (actix_web::http::StatusCode::NOT_FOUND, 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) => { tracing::error!(error = %msg, "internal error"); ( @@ -63,6 +70,27 @@ impl From for ApiError { impl From for ApiError { 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 for ApiError { + fn from(e: shanty_index::IndexError) -> Self { + ApiError::Internal(e.to_string()) + } +} + +impl From for ApiError { + fn from(e: shanty_tag::TagError) -> Self { + ApiError::Internal(e.to_string()) + } +} + +impl From for ApiError { + fn from(e: shanty_org::OrgError) -> Self { ApiError::Internal(e.to_string()) } }