use thiserror::Error;
#[derive(Debug, Error)]
pub enum Error {
#[error("invalid request: {0}")]
InvalidRequest(String),
#[error(transparent)]
Memory(#[from] post_cortex_memory::error::Error),
#[error(transparent)]
Mcp(#[from] post_cortex_mcp::error::Error),
#[error(transparent)]
Storage(#[from] post_cortex_storage::error::Error),
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error(transparent)]
External(#[from] anyhow::Error),
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
impl Error {
#[must_use]
pub fn kind(&self) -> &'static str {
match self {
Self::InvalidRequest(_) => "invalid_request",
Self::Memory(_) => "memory",
Self::Mcp(_) => "mcp",
Self::Storage(_) => "storage",
Self::Io(_) => "io",
Self::External(_) => "external",
}
}
#[must_use]
pub fn grpc_status(&self) -> tonic::Status {
match self {
Self::InvalidRequest(msg) => tonic::Status::invalid_argument(msg),
Self::Memory(post_cortex_memory::error::Error::SessionNotFound(_)) => {
tonic::Status::not_found(self.to_string())
}
Self::Memory(post_cortex_memory::error::Error::Timeout { .. }) => {
tonic::Status::deadline_exceeded(self.to_string())
}
Self::Memory(post_cortex_memory::error::Error::Backpressure { .. })
| Self::Memory(post_cortex_memory::error::Error::WorkerShutdown { .. }) => {
tonic::Status::resource_exhausted(self.to_string())
}
_ => tonic::Status::internal(self.to_string()),
}
}
#[must_use]
pub fn http_status(&self) -> u16 {
match self {
Self::InvalidRequest(_) => 400,
Self::Memory(post_cortex_memory::error::Error::SessionNotFound(_)) => 404,
Self::Memory(post_cortex_memory::error::Error::Timeout { .. }) => 504,
Self::Memory(post_cortex_memory::error::Error::Backpressure { .. })
| Self::Memory(post_cortex_memory::error::Error::WorkerShutdown { .. }) => 503,
_ => 500,
}
}
}