Skip to main content

dioxus_document/
error.rs

1use std::error::Error;
2use std::fmt::Display;
3
4/// Represents an error when evaluating JavaScript
5#[derive(Debug)]
6#[non_exhaustive]
7pub enum EvalError {
8    /// The platform does not support evaluating JavaScript.
9    Unsupported,
10
11    /// The provided JavaScript has already been ran.
12    Finished,
13
14    /// The provided JavaScript is not valid and can't be ran.
15    InvalidJs(String),
16
17    /// Represents an error communicating between JavaScript and Rust.
18    Communication(String),
19
20    /// Represents an error serializing or deserializing the result of an eval
21    Serialization(serde_json::Error),
22}
23
24impl Display for EvalError {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        match self {
27            EvalError::Unsupported => write!(
28                f,
29                "EvalError::Unsupported - eval is not supported on the current platform"
30            ),
31            EvalError::Finished => write!(f, "EvalError::Finished - eval has already ran"),
32            EvalError::InvalidJs(_) => write!(
33                f,
34                "EvalError::InvalidJs - the provided javascript is invalid"
35            ),
36            EvalError::Communication(_) => write!(
37                f,
38                "EvalError::Communication - there was an error trying to communicate with between javascript and rust"
39            ),
40            EvalError::Serialization(_) => write!(
41                f,
42                "EvalError::Serialization - there was an error trying to serialize or deserialize the result of an eval"
43            ),
44        }
45    }
46}
47
48impl Error for EvalError {}