actor_kit/error.rs
1//! Error types for the actor runtime.
2
3use std::borrow::Cow;
4
5/// Convenience alias used throughout the runtime.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// The error type for the actor runtime.
9///
10/// Deliberately small: the runtime only needs to express actor-lifecycle
11/// failures, resource exhaustion (mailbox full, quota rejected), serialization
12/// failures, and internal bugs.
13#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
14pub enum Error {
15 /// An actor lifecycle or messaging error (unknown actor, stopped actor,
16 /// duplicate registration, supervision failure, ...).
17 #[error("actor error: {0}")]
18 Actor(String),
19 /// A resource limit was hit: mailbox full, spawn rejected by the
20 /// [`crate::ResourcePolicy`], message rate exceeded.
21 #[error("resource exhausted: {0}")]
22 ResourceExhausted(String),
23 /// Serialization or deserialization failure.
24 #[error("serialization failed: {0}")]
25 Serialization(String),
26 /// An internal invariant was violated (bug).
27 #[error("internal error: {0}")]
28 Internal(String),
29}
30
31impl Error {
32 /// An actor lifecycle or messaging error.
33 pub fn actor(message: impl Into<Cow<'static, str>>) -> Self {
34 Self::Actor(message.into().into_owned())
35 }
36
37 /// A resource exhaustion error (mailbox full, quota rejected).
38 pub fn resource_exhausted(message: impl Into<Cow<'static, str>>) -> Self {
39 Self::ResourceExhausted(message.into().into_owned())
40 }
41
42 /// A serialization/deserialization error.
43 pub fn serialization(message: impl Into<Cow<'static, str>>) -> Self {
44 Self::Serialization(message.into().into_owned())
45 }
46
47 /// An internal error (bug or unimplemented feature).
48 pub fn internal(message: impl Into<Cow<'static, str>>) -> Self {
49 Self::Internal(message.into().into_owned())
50 }
51}