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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use std::error::Error;
use std::fmt;
use std::fmt::Display;
use std::ptr;

use env::NapiEnv;
use sys::{napi_create_error, napi_create_range_error, napi_create_type_error,
          napi_status, napi_value};
use value::{NapiString, NapiValue};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NapiErrorKind {
    InvalidArg,
    ObjectExpected,
    StringExpected,
    NameExpected,
    FunctionExpected,
    NumberExpected,
    BooleanExpected,
    ArrayExpected,
    GenericFailure,
    PendingException,
    Cancelled,
    EscapeCalledTwice,
    ApplicationError,
}

#[derive(Clone, Debug)]
pub struct NapiError {
    pub kind: NapiErrorKind,
    pub message: Option<String>,
    pub exception: Option<napi_value>,
}

pub type NapiResult<T> = Result<T, NapiError>;

impl NapiErrorKind {
    pub fn from_napi_status(status: napi_status) -> Self {
        match status {
            napi_status::napi_invalid_arg => NapiErrorKind::InvalidArg,
            napi_status::napi_object_expected => NapiErrorKind::ObjectExpected,
            napi_status::napi_string_expected => NapiErrorKind::StringExpected,
            napi_status::napi_name_expected => NapiErrorKind::NameExpected,
            napi_status::napi_function_expected => {
                NapiErrorKind::FunctionExpected
            }
            napi_status::napi_number_expected => NapiErrorKind::NumberExpected,
            napi_status::napi_boolean_expected => {
                NapiErrorKind::BooleanExpected
            }
            napi_status::napi_array_expected => NapiErrorKind::ArrayExpected,
            napi_status::napi_generic_failure => NapiErrorKind::GenericFailure,
            napi_status::napi_pending_exception => {
                NapiErrorKind::PendingException
            }
            napi_status::napi_cancelled => NapiErrorKind::Cancelled,
            napi_status::napi_escape_called_twice => {
                NapiErrorKind::EscapeCalledTwice
            }
            _ => {
                // Both situations should never happen, so just panic.
                panic!(
                    "Either the JavaScript VM returned an unknown status code, \
                     or NapiErrorKind::from_napi_status was called with \
                     napi_status::napi_ok"
                );
            }
        }
    }
}

impl Error for NapiError {
    fn description(&self) -> &str {
        match self.kind {
            NapiErrorKind::InvalidArg => "NapiError: invalid argument",
            NapiErrorKind::ObjectExpected => "NapiError: object expected",
            NapiErrorKind::StringExpected => "NapiError: string expected",
            NapiErrorKind::NameExpected => "NapiError: name expected",
            NapiErrorKind::FunctionExpected => "NapiError: function expected",
            NapiErrorKind::NumberExpected => "NapiError: number expected",
            NapiErrorKind::BooleanExpected => "NapiError: boolean argument",
            NapiErrorKind::ArrayExpected => "NapiError: array expected",
            NapiErrorKind::GenericFailure => "NapiError: generic failure",
            NapiErrorKind::PendingException => "NapiError: pending exception",
            NapiErrorKind::Cancelled => "NapiError: cancelled",
            NapiErrorKind::EscapeCalledTwice => {
                "NapiError: escape called twice"
            }
            NapiErrorKind::ApplicationError => "NapiError: application error",
        }
    }
}

impl Display for NapiError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "{}", self.description())
            .and_then(|result| {
                if let Some(ref message) = self.message {
                    write!(formatter, " ({})", message)
                } else {
                    Ok(result)
                }
            })
            .and_then(|result| {
                if self.exception.is_some() {
                    write!(formatter, ", JavaScript exception attached")
                } else {
                    Ok(result)
                }
            })
    }
}

macro_rules! error_constructor {
    ($name:ident => $napi_fn_name:ident) => {
        pub fn $name(env: &NapiEnv, message: &NapiString) -> NapiError {
            let mut exception = ptr::null_mut();
            let status = unsafe {
                $napi_fn_name(
                    env.as_sys_env(),
                    ptr::null_mut(),
                    message.as_sys_value(),
                    &mut exception,
                )
            };

            if let Err(error) = env.handle_status(status) {
                return error;
            }

            NapiError {
                kind: NapiErrorKind::ApplicationError,
                message: None,
                exception: Some(exception),
            }
        }
    }
}

impl NapiError {
    error_constructor!(error => napi_create_error);
    error_constructor!(type_error => napi_create_type_error);
    error_constructor!(range_error => napi_create_range_error);
}