Skip to content
Clone
2f11d69
81 lines 2.8 KB Rust
use std::fmt;
use sandbox_rpc::SandboxRpcError;
#[derive(Debug)]
pub enum AgentError {
BadRequest(String),
Unauthorized,
Forbidden,
NotFound,
Conflict(String),
RateLimited(String),
RuntimeUnavailable(String),
Internal(String),
}
impl fmt::Display for AgentError {
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::RuntimeUnavailable(message) => write!(f, "{message}"),
Self::Internal(message) => write!(f, "{message}"),
}
}
}
impl std::error::Error for AgentError {}
export::db_error_conversions!(AgentError);
export::export_error_conversion!(AgentError);
export::json_payload_conversion!(AgentError);
export::domain_error_passthrough!(AgentError from space::errors::SpaceError);
impl From<SandboxRpcError> for AgentError {
fn from(error: SandboxRpcError) -> Self {
match error {
SandboxRpcError::Unavailable(message) => Self::RuntimeUnavailable(message),
SandboxRpcError::Rejected(message) => Self::BadRequest(message),
SandboxRpcError::Config(message) => Self::RuntimeUnavailable(message),
SandboxRpcError::Internal(message) => Self::Internal(message),
}
}
}
export::api_error! {
log: "agents",
pub enum ApiError {
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"),
ServiceUnavailable(503, "agent runtime unavailable"),
Internal(500, "internal server error"),
}
}
impl From<AgentError> for ApiError {
fn from(error: AgentError) -> Self {
match error {
AgentError::BadRequest(msg) => Self::bad_request(msg),
AgentError::Unauthorized => Self::unauthorized(),
AgentError::Forbidden => Self::forbidden(),
AgentError::NotFound => Self::not_found("agent resource not found"),
AgentError::Conflict(msg) => Self::conflict(msg),
AgentError::RateLimited(msg) => Self::rate_limited(msg),
AgentError::RuntimeUnavailable(message) => Self::build(
503,
export::rest::AppResponseData::err(503, message)
.with_error_code("runtime_unavailable"),
),
AgentError::Internal(message) => Self::internal(message),
}
}
}