7f01cd4
use std::fmt;
#[derive(Debug)]
pub enum SagentError {
BadRequest(String),
Unauthorized,
Forbidden,
NotFound,
Conflict(String),
RateLimited(String),
Upstream(String),
Unavailable(String),
Internal(String),
}
impl fmt::Display for SagentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::BadRequest(message) => write!(f, "{message}"),
Self::Unauthorized => write!(f, "unauthorized"),
Self::Forbidden => write!(f, "forbidden"),
Self::NotFound => write!(f, "not found"),
Self::Conflict(message) => write!(f, "{message}"),
Self::RateLimited(message) => write!(f, "{message}"),
Self::Upstream(message) => write!(f, "upstream rejected the request: {message}"),
Self::Unavailable(message) => write!(f, "agent gateway unavailable: {message}"),
Self::Internal(message) => write!(f, "{message}"),
}
}
}
impl std::error::Error for SagentError {}
export::db_error_conversions!(SagentError);
export::export_error_conversion!(SagentError);
export::json_payload_conversion!(SagentError);
export::domain_error_passthrough!(SagentError from space::errors::SpaceError);
export::api_error! {
log: "sagent",
pub enum SagentApiError {
BadRequest(400, "request failed validation"),
Unauthorized(401, "authentication required"),
Forbidden(403, "operation not allowed"),
NotFound(404, "resource not found"),
Conflict(409, "resource conflict"),
TooManyRequests(429, "rate limit exceeded"),
Internal(500, "internal server error"),
BadGateway(502, "agent gateway rejected the request"),
ServiceUnavailable(503, "agent gateway unavailable"),
}
}
impl From<SagentError> for SagentApiError {
fn from(error: SagentError) -> Self {
match error {
SagentError::BadRequest(msg) => Self::bad_request(msg),
SagentError::Unauthorized => Self::unauthorized(),
SagentError::Forbidden => Self::forbidden(),
SagentError::NotFound => Self::not_found("sagent resource not found"),
SagentError::Conflict(msg) => Self::conflict(msg),
SagentError::RateLimited(msg) => Self::rate_limited(msg),
SagentError::Upstream(msg) => Self::build(
502,
export::rest::AppResponseData::err(502, msg).with_error_code("gateway_rejected"),
),
SagentError::Unavailable(msg) => Self::build(
503,
export::rest::AppResponseData::err(503, msg).with_error_code("gateway_unavailable"),
),
SagentError::Internal(message) => Self::internal(message),
}
}
}