Skip to main content

quickjs_oxide/engine/api/core/
error.rs

1//! The API's error type.
2
3use std::fmt;
4
5use super::value::Value;
6use crate::engine::api::error::Error;
7use crate::engine::value::JsStringError;
8
9/// Why an API call failed.
10#[derive(Clone, Debug, PartialEq)]
11pub enum RuntimeError {
12    /// JavaScript threw this value.
13    Exception(Value),
14    /// An API call was made through a `Context` or `Runtime` from inside a
15    /// host function or callback, which has its own scope.
16    Reentered,
17    /// The script was interrupted ([`InterruptHandle`](super::InterruptHandle)
18    /// or [`Host::interrupt`](super::Host::interrupt)); no `catch` or
19    /// `finally` of it ran.
20    Interrupted,
21    /// A host function panicked while the runtime ran it; the runtime's
22    /// state is unknown and every later call fails.
23    Poisoned,
24    /// The engine does not have this (`"realms"`).
25    Unsupported(&'static str),
26    /// The front end rejected the source.
27    Compile(Error),
28    /// The heap cap was reached.
29    OutOfMemory,
30    /// The embedder broke one of the API's preconditions.
31    Invariant(&'static str),
32    /// The engine failed internally.
33    Internal(String),
34}
35
36impl fmt::Display for RuntimeError {
37    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38        match self {
39            RuntimeError::Exception(_) => f.write_str("JavaScript exception"),
40            RuntimeError::Reentered => {
41                f.write_str("the runtime was re-entered from a host callback")
42            }
43            RuntimeError::Interrupted => f.write_str("interrupted"),
44            RuntimeError::Poisoned => {
45                f.write_str("the runtime is poisoned: a host function panicked")
46            }
47            RuntimeError::Unsupported(what) => write!(f, "{what} are not supported"),
48            RuntimeError::Compile(error) => write!(f, "{error}"),
49            RuntimeError::OutOfMemory => f.write_str("out of memory"),
50            RuntimeError::Invariant(what) => write!(f, "invariant violated: {what}"),
51            RuntimeError::Internal(message) => write!(f, "internal error: {message}"),
52        }
53    }
54}
55
56impl std::error::Error for RuntimeError {}
57
58impl From<Error> for RuntimeError {
59    fn from(error: Error) -> Self {
60        RuntimeError::Compile(error)
61    }
62}
63
64impl From<JsStringError> for RuntimeError {
65    fn from(error: JsStringError) -> Self {
66        match error {
67            JsStringError::OutOfMemory => RuntimeError::OutOfMemory,
68            other => RuntimeError::Internal(other.to_string()),
69        }
70    }
71}
72
73impl From<oxide_core::HeapError> for RuntimeError {
74    fn from(error: oxide_core::HeapError) -> Self {
75        match error {
76            oxide_core::HeapError::OutOfMemory => RuntimeError::OutOfMemory,
77            other => RuntimeError::Internal(format!("{other:?}")),
78        }
79    }
80}