1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::panic::{catch_unwind, UnwindSafe};
use neon_runtime;
use neon_runtime::raw;
use crate::context::internal::Env;
use crate::context::Context;
use crate::result::{NeonResult, Throw};
use crate::types::internal::ValueInternal;
use crate::types::utf8::Utf8;
use crate::types::{build, Handle, Managed, Object, Value};
#[repr(C)]
#[derive(Clone, Copy)]
pub struct JsError(raw::Local);
impl Managed for JsError {
fn to_raw(self) -> raw::Local {
self.0
}
fn from_raw(_: Env, h: raw::Local) -> Self {
JsError(h)
}
}
impl ValueInternal for JsError {
fn name() -> String {
"Error".to_string()
}
fn is_typeof<Other: Value>(env: Env, other: Other) -> bool {
unsafe { neon_runtime::tag::is_error(env.to_raw(), other.to_raw()) }
}
}
impl Value for JsError {}
impl Object for JsError {}
impl JsError {
pub fn error<'a, C: Context<'a>, S: AsRef<str>>(
cx: &mut C,
msg: S,
) -> NeonResult<Handle<'a, JsError>> {
let msg = cx.string(msg.as_ref());
build(cx.env(), |out| unsafe {
neon_runtime::error::new_error(cx.env().to_raw(), out, msg.to_raw());
true
})
}
pub fn type_error<'a, C: Context<'a>, S: AsRef<str>>(
cx: &mut C,
msg: S,
) -> NeonResult<Handle<'a, JsError>> {
let msg = cx.string(msg.as_ref());
build(cx.env(), |out| unsafe {
neon_runtime::error::new_type_error(cx.env().to_raw(), out, msg.to_raw());
true
})
}
pub fn range_error<'a, C: Context<'a>, S: AsRef<str>>(
cx: &mut C,
msg: S,
) -> NeonResult<Handle<'a, JsError>> {
let msg = cx.string(msg.as_ref());
build(cx.env(), |out| unsafe {
neon_runtime::error::new_range_error(cx.env().to_raw(), out, msg.to_raw());
true
})
}
}
pub(crate) fn convert_panics<T, F: UnwindSafe + FnOnce() -> NeonResult<T>>(
env: Env,
f: F,
) -> NeonResult<T> {
match catch_unwind(|| f()) {
Ok(result) => result,
Err(panic) => {
let msg = if let Some(string) = panic.downcast_ref::<String>() {
format!("internal error in Neon module: {}", string)
} else if let Some(str) = panic.downcast_ref::<&str>() {
format!("internal error in Neon module: {}", str)
} else {
"internal error in Neon module".to_string()
};
let (data, len) = Utf8::from(&msg[..]).truncate().lower();
unsafe {
#[cfg(feature = "napi-1")]
neon_runtime::error::clear_exception(env.to_raw());
neon_runtime::error::throw_error_from_utf8(env.to_raw(), data, len);
Err(Throw)
}
}
}
}