use std::error::Error;
use std::fmt;
use std::sync::Arc;
use crate::runtime::hook::{self, AttributedError};
use crate::runtime::model::RequestError;
use crate::runtime::session::LeaseError;
use crate::transcript::store;
use crate::value::lifecycle::CoordError;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ClosePanickedError;
impl fmt::Display for ClosePanickedError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("Agent OnClose callback panicked")
}
}
impl Error for ClosePanickedError {}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ConfigError {
Hooks(hook::RegistryError),
InvalidDefaults(RequestError),
RevisionIdExhausted,
}
impl fmt::Display for ConfigError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Hooks(error) => error.fmt(formatter),
Self::InvalidDefaults(error) => error.fmt(formatter),
Self::RevisionIdExhausted => formatter.write_str("Agent revision identity exhausted"),
}
}
}
impl Error for ConfigError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Hooks(error) => Some(error),
Self::InvalidDefaults(error) => Some(error),
Self::RevisionIdExhausted => None,
}
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum StartError {
AgentClosed,
SessionBusy,
Cancelled,
RunIdExhausted,
InvalidIdentity(CoordError),
Admission(LeaseError),
Prepare(AttributedError),
Transcript(store::Error),
}
impl fmt::Display for StartError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AgentClosed => formatter.write_str("Agent is closed"),
Self::SessionBusy => formatter.write_str("Session already has an active Run"),
Self::Cancelled => formatter.write_str("Run admission canceled"),
Self::RunIdExhausted => formatter.write_str("Run identity exhausted"),
Self::InvalidIdentity(error) => error.fmt(formatter),
Self::Admission(error) => error.fmt(formatter),
Self::Prepare(error) => {
write!(formatter, "hook {} prepare failed", error.name())
}
Self::Transcript(error) => error.fmt(formatter),
}
}
}
impl Error for StartError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::InvalidIdentity(error) => Some(error),
Self::Admission(error) => Some(error),
Self::Prepare(error) => Some(error),
Self::Transcript(error) => Some(error),
Self::AgentClosed | Self::SessionBusy | Self::Cancelled | Self::RunIdExhausted => None,
}
}
}
#[derive(Clone, Debug)]
pub struct TeardownError(pub(super) Arc<[AttributedError]>);
impl TeardownError {
#[must_use]
pub fn entries(&self) -> &[AttributedError] {
&self.0
}
}
impl fmt::Display for TeardownError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"{} Agent OnClose callback(s) failed",
self.0.len()
)
}
}
impl Error for TeardownError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.0.first().map(|error| error as &(dyn Error + 'static))
}
}
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum CloseError {
#[non_exhaustive]
Busy {
active_runs: usize,
},
Hooks(TeardownError),
}
impl fmt::Display for CloseError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Busy { active_runs } => {
write!(formatter, "Agent has {active_runs} active Run(s)")
}
Self::Hooks(errors) => errors.fmt(formatter),
}
}
}
impl Error for CloseError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Busy { .. } => None,
Self::Hooks(errors) => Some(errors),
}
}
}