1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
use core::fmt::{Formatter, Write};
use kameo::{
Actor,
actor::{ActorRef, WeakActorRef},
error::SendError,
};
pub(crate) trait ResultExt {
type T;
/// Attach actor info to this result if it's an error.
fn with_actor_info(self, aref: impl Into<ActorInfo>) -> Result<Self::T, Error>;
}
impl<T, E> ResultExt for Result<T, E>
where
E: Into<Error>,
{
type T = T;
fn with_actor_info(self, aref: impl Into<ActorInfo>) -> Result<T, Error> {
self.map_err(|e| e.into().with_actor_info(aref))
}
}
/// Errors that may occur while executing or interacting with the runtime.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Error {
/// Kind of error this is.
pub kind: ErrorKind,
/// Rust type name of sent message that caused the error.
///
/// May not be populated if this was not generated as a result of sending a message.
pub message_ty: Option<&'static str>,
/// Actor to which the message was being sent (optional).
///
/// May not be populated if either this wasn't from an actor message, or the actor ref
/// wasn't available when the error was constructed.
pub target_actor: Option<ActorInfo>,
}
impl Error {
/// Attach information about a destination actor to this error.
///
/// Typically, `aref` will be `&ActorRef`.
pub fn with_actor_info(self, aref: impl Into<ActorInfo>) -> Self {
Self {
target_actor: Some(aref.into()),
..self
}
}
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
// format is "sending MESSAGE_TYPE to ACTOR: ERROR_DESC", the complexity is to account for
// independently-optional fields
if let Some(ty) = self.message_ty {
write!(f, "sending {ty}")?;
if self.target_actor.is_some() {
f.write_char(' ')?;
}
}
if let Some(actor) = &self.target_actor {
write!(f, "{actor}")?;
}
if self.message_ty.is_some() || self.target_actor.is_some() {
f.write_str(": ")?;
}
self.kind.fmt(f)
}
}
impl core::error::Error for Error {}
impl<M, E> From<SendError<M, E>> for Error {
fn from(err: SendError<M, E>) -> Self {
Self {
kind: err.into(),
message_ty: Some(core::any::type_name::<M>()),
target_actor: None,
}
}
}
/// Info about an actor that caused an error.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct ActorInfo {
/// The Rust type name of the actor.
pub ty: &'static str,
/// The actor's unique id.
pub id: kameo::actor::ActorId,
}
impl<A> From<&ActorRef<A>> for ActorInfo
where
A: Actor,
{
fn from(aref: &ActorRef<A>) -> Self {
Self {
ty: core::any::type_name::<A>(),
id: aref.id(),
}
}
}
impl<A> From<ActorRef<A>> for ActorInfo
where
A: Actor,
{
fn from(aref: ActorRef<A>) -> Self {
Self::from(&aref)
}
}
impl<A> From<&WeakActorRef<A>> for ActorInfo
where
A: Actor,
{
fn from(aref: &WeakActorRef<A>) -> Self {
Self {
ty: core::any::type_name::<A>(),
id: aref.id(),
}
}
}
impl<A> From<WeakActorRef<A>> for ActorInfo
where
A: Actor,
{
fn from(aref: WeakActorRef<A>) -> Self {
Self::from(&aref)
}
}
impl core::fmt::Display for ActorInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
write!(f, "{}({})", self.ty, self.id)
}
}
/// Kinds of errors that may occur while running the runtime.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ErrorKind {
/// An actor that was expected to be available was unreachable, probably because it
/// either panicked or exited. It's unlikely this operation can be retried successfully.
ActorGone,
/// The actor replied with an error.
ReplyErr,
/// The target actor's mailbox was full.
MailboxFull,
/// An operation timed out.
Timeout,
/// A netstack-only operation was attempted on a runtime running in TUN transport mode (no
/// userspace application netstack exists in that mode).
UnsupportedInTunMode,
/// TUN transport mode was requested but the TUN device could not be created (e.g. missing
/// root/CAP_NET_ADMIN), or the runtime was built without the `tun` feature.
TunUnavailable,
/// The network monitor was requested (`Config::network_monitor == true`) but the runtime was
/// built without the `network-monitor` feature, so the supervisor could not be spawned. A
/// config/build mismatch knowable at spawn time; never silently ignored (mirrors
/// [`TunUnavailable`](Self::TunUnavailable)).
NetworkMonitorUnavailable,
}
impl core::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Self::ActorGone => write!(f, "expected actor is unreachable"),
Self::ReplyErr => write!(f, "actor replied with an error"),
Self::MailboxFull => write!(f, "actor's mailbox was full"),
Self::Timeout => write!(f, "operation timed out"),
Self::UnsupportedInTunMode => {
write!(f, "netstack operation unsupported in TUN transport mode")
}
Self::TunUnavailable => write!(f, "TUN transport unavailable"),
Self::NetworkMonitorUnavailable => write!(
f,
"network monitor requested but the `network-monitor` feature is not enabled"
),
}
}
}
impl<M, E> From<SendError<M, E>> for ErrorKind {
fn from(err: SendError<M, E>) -> Self {
match err {
SendError::ActorNotRunning(_) | SendError::ActorStopped => ErrorKind::ActorGone,
SendError::HandlerError(_) => ErrorKind::ReplyErr,
SendError::MailboxFull(..) => ErrorKind::MailboxFull,
SendError::Timeout(_) => ErrorKind::Timeout,
}
}
}