Skip to main content

workflow_node/
error.rs

1//! Errors produced by the the [`node`](super) crate
2use thiserror::Error;
3use wasm_bindgen::prelude::*;
4use workflow_core::channel::{RecvError, SendError, TryRecvError};
5use workflow_wasm::printable::Printable;
6
7/// Errors produced by the [`node`](crate) crate.
8#[derive(Debug, Error)]
9pub enum Error {
10    /// A process or task was started while it was already running.
11    #[error("Already running")]
12    AlreadyRunning,
13    /// An operation requiring a running task was attempted while it was stopped.
14    #[error("The task is not running")]
15    NotRunning,
16    /// The underlying child process reference is missing.
17    #[error("Child process reference is absent")]
18    ProcIsAbsent,
19    /// Failure delivering a value over a channel.
20    #[error("{0:?}")]
21    Send(String),
22    /// Failure receiving a value from a channel.
23    #[error("{0:?}")]
24    Recv(#[from] RecvError),
25    /// Failure on a non-blocking channel receive attempt.
26    #[error("{0:?}")]
27    TryRecv(#[from] TryRecvError),
28    /// Error propagated from the [`workflow_task`] task framework.
29    #[error(transparent)]
30    Task(#[from] workflow_task::TaskError),
31    /// Error propagated from a WASM [`callback`](workflow_wasm::callback).
32    #[error(transparent)]
33    Callback(#[from] workflow_wasm::callback::CallbackError),
34    /// A JavaScript value thrown from interop, wrapped for display.
35    #[error("{0}")]
36    JsValue(Printable),
37}
38
39unsafe impl Send for Error {}
40unsafe impl Sync for Error {}
41
42impl<T> From<SendError<T>> for Error {
43    fn from(err: SendError<T>) -> Self {
44        Error::Send(err.to_string())
45    }
46}
47
48impl From<JsValue> for Error {
49    fn from(err: JsValue) -> Self {
50        Error::JsValue(Printable::new(err))
51    }
52}