tiny_actor/error/
mod.rs

1//! This module contains all errors
2
3use concurrent_queue::PushError;
4use std::any::Any;
5use thiserror::Error;
6
7/// Error returned when `Stream`ing an [Inbox].
8///
9/// Process has been halted and should now exit.
10#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
11#[error("Process has been halted")]
12pub struct HaltedError;
13
14/// Error returned when receiving a message from an inbox.
15#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
16pub enum RecvError {
17    /// Process has been halted and should now exit.
18    #[error("Couldn't receive because the process has been halted")]
19    Halted,
20    /// Channel has been closed, and contains no more messages. It is impossible for new
21    /// messages to be sent to the channel.
22    #[error("Couldn't receive becuase the channel is closed and empty")]
23    ClosedAndEmpty,
24}
25
26/// Error returned when receiving a message from an inbox.
27#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
28pub enum TryRecvError {
29    /// Process has been halted and should now exit.
30    #[error("Couldn't receive because the process has been halted")]
31    Halted,
32    /// The channel is empty, but is not yet closed. New messges may arrive
33    #[error("Couldn't receive because the channel is empty")]
34    Empty,
35    /// Channel has been closed, and contains no more messages. It is impossible for new
36    /// messages to be sent to the channel.
37    #[error("Couldn't receive becuase the channel is closed and empty")]
38    ClosedAndEmpty,
39}
40
41/// An error returned when trying to send a message into a channel.
42#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
43pub enum TrySendError<M> {
44    /// The channel has been closed, and no longer accepts new messages.
45    #[error("Couldn't send message because Channel is closed")]
46    Closed(M),
47    /// The channel is full.
48    #[error("Couldn't send message because Channel is full")]
49    Full(M),
50}
51
52impl<M> From<PushError<M>> for TrySendError<M> {
53    fn from(e: PushError<M>) -> Self {
54        match e {
55            PushError::Full(msg) => Self::Full(msg),
56            PushError::Closed(msg) => Self::Closed(msg),
57        }
58    }
59}
60
61/// Error returned when sending a message into a channel.
62///
63/// The channel has been closed, and no longer accepts new messages.
64#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
65pub struct SendError<M>(pub M);
66
67/// An error returned when trying to spawn more processes onto a channel
68#[derive(Clone, PartialEq, Eq, Hash, Error)]
69pub enum TrySpawnError<T> {
70    /// The actor has exited.
71    #[error("Couldn't spawn process because the actor has exited")]
72    Exited(T),
73    /// The spawned inbox does not have the correct type
74    #[error("Couldn't spawn process because the given inbox-type is incorrect")]
75    IncorrectType(T),
76}
77
78impl<T> std::fmt::Debug for TrySpawnError<T> {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        match self {
81            Self::Exited(_) => f.debug_tuple("Exited").finish(),
82            Self::IncorrectType(_) => f.debug_tuple("IncorrectType").finish(),
83        }
84    }
85}
86
87/// An error returned when spawning more processes onto a channel.
88///
89/// The actor has exited.
90#[derive(Clone, PartialEq, Eq, Hash, Error)]
91#[error("Couldn't spawn process because the channel has exited")]
92pub struct SpawnError<T>(pub T);
93
94impl<T> std::fmt::Debug for SpawnError<T> {
95    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96        f.debug_tuple("SpawnError").finish()
97    }
98}
99
100/// An error returned from an exiting process.
101#[derive(Debug, Error)]
102pub enum ExitError {
103    /// Process panicked.
104    #[error("Process has exited because of a panic")]
105    Panic(Box<dyn Any + Send>),
106    /// Process  was aborted.
107    #[error("Process has exited because it was aborted")]
108    Abort,
109}
110
111impl ExitError {
112    /// Whether the error is a panic.
113    pub fn is_panic(&self) -> bool {
114        match self {
115            ExitError::Panic(_) => true,
116            ExitError::Abort => false,
117        }
118    }
119
120    /// Whether the error is an abort.
121    pub fn is_abort(&self) -> bool {
122        match self {
123            ExitError::Panic(_) => false,
124            ExitError::Abort => true,
125        }
126    }
127}
128
129impl From<tokio::task::JoinError> for ExitError {
130    fn from(e: tokio::task::JoinError) -> Self {
131        match e.try_into_panic() {
132            Ok(panic) => ExitError::Panic(panic),
133            Err(_) => ExitError::Abort,
134        }
135    }
136}