rsactor/error.rs
1// Copyright 2022 Jeff Kim <hiking90@gmail.com>
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::Identity;
5use std::time::Duration;
6
7#[derive(Debug)]
8/// Represents errors that can occur in the rsactor framework.
9///
10/// These errors may be encountered during various actor operations, such as sending messages
11/// with [`tell`](crate::actor_ref::ActorRef::tell) or [`ask`](crate::actor_ref::ActorRef::ask),
12/// or during actor lifecycle operations like [`spawn`](crate::spawn).
13pub enum Error {
14 /// Error when sending a message to an actor
15 Send {
16 /// ID of the actor that failed to receive the message
17 identity: Identity,
18 /// Additional context about the error
19 details: String,
20 },
21 /// Error when receiving a response from an actor
22 Receive {
23 /// ID of the actor that failed to send a response
24 identity: Identity,
25 /// Additional context about the error
26 details: String,
27 },
28 /// Error when a request times out
29 Timeout {
30 /// ID of the actor that timed out
31 identity: Identity,
32 /// The duration after which the request timed out
33 timeout: Duration,
34 /// Type of operation that timed out (e.g., "send", "ask")
35 operation: String,
36 },
37 /// Error when a message type is not handled by an actor
38 UnhandledMessageType {
39 /// ID of the actor that timed out
40 identity: Identity,
41 /// Expected message types
42 expected_types: Vec<&'static str>,
43 /// Actual message type ID
44 actual_type_id: std::any::TypeId,
45 },
46 /// Error when downcasting a reply to the expected type
47 Downcast {
48 /// ID of the actor that sent the incompatible reply
49 identity: Identity,
50 expected_type: String,
51 },
52 /// Error when a runtime operation fails
53 Runtime {
54 /// ID of the actor where the runtime error occurred
55 identity: Identity,
56 /// Additional context about the error
57 details: String,
58 },
59 /// Error related to mailbox capacity configuration
60 MailboxCapacity {
61 /// The error message
62 message: String,
63 },
64}
65
66impl std::fmt::Display for Error {
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 match self {
69 Error::Send { identity: actor_id, details } => {
70 write!(f, "Failed to send message to actor {}: {}", actor_id.name(), details)
71 }
72 Error::Receive { identity: actor_id, details } => {
73 write!(f, "Failed to receive reply from actor {}: {}", actor_id.name(), details)
74 }
75 Error::Timeout { identity: actor_id, timeout, operation } => {
76 write!(f, "{} operation to actor {} timed out after {:?}",
77 operation, actor_id.name(), timeout)
78 }
79 Error::UnhandledMessageType { identity: actor_id, expected_types, actual_type_id } => {
80 write!(
81 f,
82 "Actor '{}' received an unhandled message type. Expected one of: [{}]. Actual message type ID: {:?}",
83 actor_id.name(),
84 expected_types.join(", "),
85 actual_type_id
86 )
87 }
88 Error::Downcast { identity: actor_id, expected_type } => {
89 write!(f, "Failed to downcast reply from actor {} to expected type '{}'",
90 actor_id.name(), expected_type)
91 }
92 Error::Runtime { identity: actor_id, details } => {
93 write!(f, "Runtime error in actor {}: {}", actor_id.name(), details)
94 }
95 Error::MailboxCapacity { message } => {
96 write!(f, "Mailbox capacity error: {}", message)
97 }
98 }
99 }
100}
101
102impl std::error::Error for Error {}
103
104/// A Result type specialized for rsactor operations.
105///
106/// This type is returned by most actor operations like [`tell`](crate::actor_ref::ActorRef::tell),
107/// [`ask`](crate::actor_ref::ActorRef::ask), [`stop`](crate::actor_ref::ActorRef::stop), etc.
108pub type Result<T> = std::result::Result<T, Error>;