Skip to main content

wasm_actor_bridge/
error.rs

1//! Typed error hierarchy for the bridge.
2
3/// Errors from the worker bridge.
4#[derive(Debug, thiserror::Error)]
5pub enum BridgeError {
6    /// `Worker::new()` failed (invalid script URL, CSP, etc.).
7    #[error("worker spawn failed: {0}")]
8    Spawn(String),
9
10    /// Serialisation of a command or event failed.
11    #[error("serialisation failed: {0}")]
12    Serialisation(String),
13
14    /// `postMessage` rejected by the browser.
15    #[error("postMessage failed: {0}")]
16    PostMessage(String),
17
18    /// Script-level error (wrong URL, CSP, syntax error in worker script).
19    /// Not automatically recoverable — the script itself is broken.
20    #[error("worker script error: {0}")]
21    WorkerError(String),
22
23    /// Worker process crashed (WASM panic, OOM, unreachable trap).
24    /// A Supervisor should attempt respawn.
25    #[error("worker crashed")]
26    WorkerCrashed,
27
28    /// The worker handle has been terminated.
29    #[error("bridge terminated")]
30    Terminated,
31
32    /// The event channel has been closed (receiver dropped).
33    #[error("event channel closed")]
34    ChannelClosed,
35
36    /// The command channel is full — the worker cannot accept more commands.
37    #[error("command channel full")]
38    ChannelFull,
39
40    /// Builder configuration is invalid.
41    #[error("invalid configuration: {0}")]
42    InvalidConfig(String),
43}
44
45impl BridgeError {
46    /// True if the error indicates the worker process died unexpectedly
47    /// and respawn may succeed.
48    pub fn is_crash(&self) -> bool {
49        matches!(self, Self::WorkerCrashed)
50    }
51
52    /// True if the error is permanent and respawn will not help.
53    pub fn is_permanent(&self) -> bool {
54        matches!(
55            self,
56            Self::Spawn(_) | Self::WorkerError(_) | Self::InvalidConfig(_)
57        )
58    }
59}