-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.rs
58 lines (51 loc) · 1.54 KB
/
error.rs
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
use crate::sys::*;
use std::ffi::CStr;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
pub enum Error {
NodeApi(napi_status, String),
Message(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let message = match self {
Error::NodeApi(_, message) => message,
Error::Message(message) => message,
};
write!(f, "{}", message)
}
}
impl std::error::Error for Error {}
impl Error {
pub fn node_api(status: napi_status, message: impl Into<String>) -> Error {
Error::NodeApi(status, message.into())
}
pub fn message(message: impl Into<String>) -> Error {
Error::Message(message.into())
}
/// Create a `NodeAPIError` value from the most recent Node-API call.
/// # Safety
pub unsafe fn from_last_node_api_error(env: napi_env, status: napi_status) -> Error {
let mut error_info = std::mem::MaybeUninit::uninit();
let last_error_status = napi_get_last_error_info(env, error_info.as_mut_ptr());
if last_error_status != napi_status::napi_ok {
napi_fatal_error(std::ptr::null(), 0, std::ptr::null(), 0);
unreachable!()
}
let error_info = error_info.assume_init();
let message = (*error_info).error_message;
let message = if !message.is_null() {
match CStr::from_ptr(message).to_str() {
Ok(message) => message,
Err(_) => {
napi_fatal_error(std::ptr::null(), 0, std::ptr::null(), 0);
unreachable!()
}
}
} else {
"empty error message"
};
let message = message.to_owned();
Error::NodeApi(status, message)
}
}