quickjs_rusty/errors/
execution_error.rs

1use std::{error, fmt};
2
3use crate::OwnedJsValue;
4
5use super::ValueError;
6
7/// Error on Javascript execution.
8#[derive(Debug)]
9pub enum ExecutionError {
10    /// Code to be executed contained zero-bytes.
11    InputWithZeroBytes,
12    /// Value conversion failed. (either input arguments or result value).
13    Conversion(ValueError),
14    /// Internal error.
15    Internal(String),
16    /// JS Exception was thrown.
17    Exception(OwnedJsValue),
18    /// JS Runtime exceeded the memory limit.
19    OutOfMemory,
20    #[doc(hidden)]
21    __NonExhaustive,
22}
23
24impl fmt::Display for ExecutionError {
25    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26        use ExecutionError::*;
27        match self {
28            InputWithZeroBytes => write!(f, "Invalid script input: code contains zero byte (\\0)"),
29            Conversion(e) => e.fmt(f),
30            Internal(e) => write!(f, "Internal error: {}", e),
31            Exception(e) => {
32                if e.is_string() {
33                    write!(f, "{}", e.to_string().unwrap())
34                } else {
35                    write!(f, "JS Exception: {:?}", e)
36                }
37            }
38            OutOfMemory => write!(f, "Out of memory: runtime memory limit exceeded"),
39            __NonExhaustive => unreachable!(),
40        }
41    }
42}
43
44impl PartialEq for ExecutionError {
45    fn eq(&self, other: &Self) -> bool {
46        let left = self.to_string();
47        let right = other.to_string();
48        left == right
49    }
50}
51
52impl error::Error for ExecutionError {}
53
54impl From<ValueError> for ExecutionError {
55    fn from(v: ValueError) -> Self {
56        ExecutionError::Conversion(v)
57    }
58}