Skip to main content

spawned_concurrency/
error.rs

1/// Errors that can occur when communicating with an actor.
2#[derive(Debug, thiserror::Error)]
3pub enum ActorError {
4    /// The actor has stopped or its mailbox channel is closed.
5    ///
6    /// Returned by `send()` and `request()` when the actor is no longer running.
7    #[error("Actor stopped")]
8    ActorStopped,
9    /// A request exceeded the timeout duration (default: 5 seconds).
10    ///
11    /// Returned by `request()` and `request_with_timeout()` when the actor
12    /// does not reply in time.
13    #[error("Request to Actor timed out")]
14    RequestTimeout,
15}
16
17impl<T> From<spawned_rt::threads::mpsc::SendError<T>> for ActorError {
18    fn from(_value: spawned_rt::threads::mpsc::SendError<T>) -> Self {
19        Self::ActorStopped
20    }
21}
22
23impl<T> From<spawned_rt::tasks::mpsc::SendError<T>> for ActorError {
24    fn from(_value: spawned_rt::tasks::mpsc::SendError<T>) -> Self {
25        Self::ActorStopped
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_error_into_std_error() {
35        let error: &dyn std::error::Error = &ActorError::ActorStopped;
36        assert_eq!(error.to_string(), "Actor stopped");
37    }
38}