use thiserror::Error;
use crate::types::ActorId;
pub type ActorResult<T> = Result<T, ActorError>;
#[derive(Debug, Error, Clone)]
pub enum ActorError {
#[error("actor logic error: {0}")]
User(String),
#[error("actor cancelled")]
Cancelled,
#[error("actor panicked: {0}")]
Panic(String),
#[error(transparent)]
Spawn(#[from] SpawnError),
}
impl ActorError {
pub fn user(message: impl Into<String>) -> Self {
Self::User(message.into())
}
}
impl From<TimerError> for ActorError {
fn from(value: TimerError) -> Self {
ActorError::User(value.to_string())
}
}
#[derive(Debug, Error, Clone)]
pub enum SendError {
#[error("mailbox closed")]
Closed,
}
#[derive(Debug, Error, Clone)]
pub enum TrySendError {
#[error("mailbox full")]
Full,
#[error("mailbox closed")]
Closed,
}
#[derive(Debug, Error, Clone)]
pub enum AskError {
#[error("mailbox closed before the request was sent")]
Closed,
#[error("actor stopped before replying")]
ResponseDropped,
#[error("actor returned error: {0}")]
Actor(ActorError),
}
#[derive(Debug, Error, Clone)]
pub enum SpawnError {
#[error("tokio runtime handle not in scope")]
MissingRuntime,
#[error("child actor `{0}` already registered")]
DuplicateChild(ActorId),
#[error("actor name `{name}` already taken in system `{system}`")]
NameTaken {
name: String,
system: String,
},
#[error("actor system `{0}` already exists")]
SystemNameTaken(String),
}
#[derive(Debug, Error, Clone)]
pub enum TimerError {
#[error("timer id not found")]
NotFound,
}
#[derive(Debug, Error, Clone)]
pub enum StreamError {
#[error("stream id not found")]
NotFound,
}
impl From<StreamError> for ActorError {
fn from(value: StreamError) -> Self {
ActorError::User(value.to_string())
}
}
#[derive(Debug, Error, Clone)]
pub enum SupervisionError {
#[error("child `{0}` not found")]
ChildNotFound(ActorId),
#[error("restart budget exhausted")]
BudgetExhausted,
#[error("factory failed: {0}")]
FactoryFailed(String),
#[error("actor is not configured as a supervisor")]
NotASupervisor,
#[error("child `{0}` is running")]
ChildRunning(ActorId),
#[error("child `{0}` is restarting")]
ChildRestarting(ActorId),
#[error("child `{0}` is unresponsive to kill")]
ChildUnresponsive(ActorId),
}
impl From<SupervisionError> for ActorError {
fn from(value: SupervisionError) -> Self {
ActorError::User(value.to_string())
}
}