2020-08-26 01:07:27 +09:00
|
|
|
use std::convert::From;
|
|
|
|
use std::error::Error as StdError;
|
2020-06-21 20:10:06 +09:00
|
|
|
use std::fmt;
|
2020-08-26 01:07:27 +09:00
|
|
|
#[cfg(feature = "serde-json")]
|
|
|
|
use std::fmt::Display;
|
2020-06-21 20:10:06 +09:00
|
|
|
use std::os::raw::c_char;
|
|
|
|
use std::ptr;
|
|
|
|
|
2020-08-26 01:07:27 +09:00
|
|
|
#[cfg(feature = "serde-json")]
|
|
|
|
use serde::{de, ser};
|
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
use crate::{sys, Status};
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Error {
|
|
|
|
pub status: Status,
|
|
|
|
pub reason: String,
|
|
|
|
}
|
|
|
|
|
2020-08-26 01:07:27 +09:00
|
|
|
impl StdError for Error {}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde-json")]
|
|
|
|
impl ser::Error for Error {
|
|
|
|
fn custom<T: Display>(msg: T) -> Self {
|
|
|
|
Error::new(Status::InvalidArg, msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "serde-json")]
|
|
|
|
impl de::Error for Error {
|
|
|
|
fn custom<T: Display>(msg: T) -> Self {
|
|
|
|
Error::new(Status::InvalidArg, msg.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(f, "{:?}: {}", self.status, self.reason)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
pub fn new(status: Status, reason: String) -> Self {
|
|
|
|
Error { status, reason }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_status(status: Status) -> Self {
|
|
|
|
Error {
|
|
|
|
status,
|
|
|
|
reason: "".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_reason(reason: String) -> Self {
|
|
|
|
Error {
|
|
|
|
status: Status::GenericFailure,
|
|
|
|
reason,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 23:57:11 +09:00
|
|
|
pub(crate) fn into_raw(self, env: sys::napi_env) -> sys::napi_value {
|
2020-06-21 20:10:06 +09:00
|
|
|
let mut err = ptr::null_mut();
|
|
|
|
let s = self.reason;
|
|
|
|
unsafe {
|
|
|
|
let mut err_reason = ptr::null_mut();
|
|
|
|
let status = sys::napi_create_string_utf8(
|
|
|
|
env,
|
|
|
|
s.as_ptr() as *const c_char,
|
2020-11-04 23:52:13 +09:00
|
|
|
s.len() as _,
|
2020-06-21 20:10:06 +09:00
|
|
|
&mut err_reason,
|
|
|
|
);
|
|
|
|
debug_assert!(
|
|
|
|
status == sys::napi_status::napi_ok,
|
|
|
|
"Create error reason failed"
|
|
|
|
);
|
|
|
|
let status = sys::napi_create_error(env, ptr::null_mut(), err_reason, &mut err);
|
|
|
|
debug_assert!(status == sys::napi_status::napi_ok, "Create error failed");
|
|
|
|
};
|
|
|
|
err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::ffi::NulError> for Error {
|
|
|
|
fn from(error: std::ffi::NulError) -> Self {
|
|
|
|
Error {
|
2020-07-08 01:59:09 +09:00
|
|
|
status: Status::GenericFailure,
|
|
|
|
reason: format!("{}", error),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(error: std::io::Error) -> Self {
|
|
|
|
Error {
|
|
|
|
status: Status::GenericFailure,
|
|
|
|
reason: format!("{}", error),
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn check_status(code: sys::napi_status) -> Result<()> {
|
|
|
|
let status = Status::from(code);
|
|
|
|
match status {
|
|
|
|
Status::Ok => Ok(()),
|
2020-08-26 01:07:27 +09:00
|
|
|
_ => Err(Error::new(status, "".to_owned())),
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
|
|
|
}
|