use crate::{kind::AppError, public::PublicError};
pub fn into_response_parts(err: &AppError) -> (u16, PublicError) {
match err {
AppError::Validation { .. } => (
400,
PublicError::new(
"invalid_request",
"The request contains invalid data.",
None,
),
),
AppError::Forbidden { .. } => (403, PublicError::new("forbidden", "Access denied.", None)),
AppError::NotFound => (
404,
PublicError::new("not_found", "The requested resource was not found.", None),
),
AppError::Conflict => (
409,
PublicError::new(
"conflict",
"The request conflicts with the current state.",
None,
),
),
AppError::Dependency { .. } => (
503,
PublicError::new(
"temporarily_unavailable",
"A required service is temporarily unavailable.",
None,
),
),
AppError::Crypto | AppError::Internal => (
500,
PublicError::new("internal_error", "An internal error occurred.", None),
),
AppError::RateLimit { .. } => (
429,
PublicError::new(
"rate_limited",
"Too many requests. Please retry later.",
None,
),
),
}
}
#[must_use]
pub fn retry_after_seconds(err: &AppError) -> Option<u64> {
match err {
AppError::RateLimit {
retry_after_seconds,
} => *retry_after_seconds,
_ => None,
}
}