quickjs_oxide/engine/api/core/
error.rs1use std::fmt;
4
5use super::value::Value;
6use crate::engine::api::error::Error;
7use crate::engine::value::JsStringError;
8
9#[derive(Clone, Debug, PartialEq)]
11pub enum RuntimeError {
12 Exception(Value),
14 Reentered,
17 Interrupted,
21 Poisoned,
24 Unsupported(&'static str),
26 Compile(Error),
28 OutOfMemory,
30 Invariant(&'static str),
32 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}