forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.cc
More file actions
178 lines (146 loc) · 4.96 KB
/
error.cc
File metadata and controls
178 lines (146 loc) · 4.96 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "napi.h"
using namespace Napi;
namespace {
void DoNotCatch(const CallbackInfo& info) {
Function thrower = info[0].As<Function>();
thrower({});
}
void ThrowApiError(const CallbackInfo& info) {
// Attempting to call an empty function value will throw an API error.
Function(info.Env(), nullptr).Call(std::initializer_list<napi_value>{});
}
#ifdef NAPI_CPP_EXCEPTIONS
void ThrowJSError(const CallbackInfo& info) {
std::string message = info[0].As<String>().Utf8Value();
throw Error::New(info.Env(), message);
}
void ThrowTypeError(const CallbackInfo& info) {
std::string message = info[0].As<String>().Utf8Value();
throw TypeError::New(info.Env(), message);
}
void ThrowRangeError(const CallbackInfo& info) {
std::string message = info[0].As<String>().Utf8Value();
throw RangeError::New(info.Env(), message);
}
Value CatchError(const CallbackInfo& info) {
Function thrower = info[0].As<Function>();
try {
thrower({});
} catch (const Error& e) {
return e.Value();
}
return info.Env().Null();
}
Value CatchErrorMessage(const CallbackInfo& info) {
Function thrower = info[0].As<Function>();
try {
thrower({});
} catch (const Error& e) {
std::string message = e.Message();
return String::New(info.Env(), message);
}
return info.Env().Null();
}
void CatchAndRethrowError(const CallbackInfo& info) {
Function thrower = info[0].As<Function>();
try {
thrower({});
} catch (Error& e) {
e.Set("caught", Boolean::New(info.Env(), true));
throw;
}
}
void ThrowErrorThatEscapesScope(const CallbackInfo& info) {
HandleScope scope(info.Env());
std::string message = info[0].As<String>().Utf8Value();
throw Error::New(info.Env(), message);
}
void CatchAndRethrowErrorThatEscapesScope(const CallbackInfo& info) {
HandleScope scope(info.Env());
try {
ThrowErrorThatEscapesScope(info);
} catch (Error& e) {
e.Set("caught", Boolean::New(info.Env(), true));
throw;
}
}
#else // NAPI_CPP_EXCEPTIONS
void ThrowJSError(const CallbackInfo& info) {
std::string message = info[0].As<String>().Utf8Value();
Error::New(info.Env(), message).ThrowAsJavaScriptException();
}
void ThrowTypeError(const CallbackInfo& info) {
std::string message = info[0].As<String>().Utf8Value();
TypeError::New(info.Env(), message).ThrowAsJavaScriptException();
}
void ThrowRangeError(const CallbackInfo& info) {
std::string message = info[0].As<String>().Utf8Value();
RangeError::New(info.Env(), message).ThrowAsJavaScriptException();
}
Value CatchError(const CallbackInfo& info) {
Function thrower = info[0].As<Function>();
thrower({});
Env env = info.Env();
if (env.IsExceptionPending()) {
Error e = env.GetAndClearPendingException();
return e.Value();
}
return info.Env().Null();
}
Value CatchErrorMessage(const CallbackInfo& info) {
Function thrower = info[0].As<Function>();
thrower({});
Env env = info.Env();
if (env.IsExceptionPending()) {
Error e = env.GetAndClearPendingException();
std::string message = e.Message();
return String::New(env, message);
}
return info.Env().Null();
}
void CatchAndRethrowError(const CallbackInfo& info) {
Function thrower = info[0].As<Function>();
thrower({});
Env env = info.Env();
if (env.IsExceptionPending()) {
Error e = env.GetAndClearPendingException();
e.Set("caught", Boolean::New(info.Env(), true));
e.ThrowAsJavaScriptException();
}
}
void ThrowErrorThatEscapesScope(const CallbackInfo& info) {
HandleScope scope(info.Env());
std::string message = info[0].As<String>().Utf8Value();
Error::New(info.Env(), message).ThrowAsJavaScriptException();
}
void CatchAndRethrowErrorThatEscapesScope(const CallbackInfo& info) {
HandleScope scope(info.Env());
ThrowErrorThatEscapesScope(info);
Env env = info.Env();
if (env.IsExceptionPending()) {
Error e = env.GetAndClearPendingException();
e.Set("caught", Boolean::New(info.Env(), true));
e.ThrowAsJavaScriptException();
}
}
#endif // NAPI_CPP_EXCEPTIONS
void ThrowFatalError(const CallbackInfo& /*info*/) {
Error::Fatal("Error::ThrowFatalError", "This is a fatal error");
}
} // end anonymous namespace
Object InitError(Env env) {
Object exports = Object::New(env);
exports["throwApiError"] = Function::New(env, ThrowApiError);
exports["throwJSError"] = Function::New(env, ThrowJSError);
exports["throwTypeError"] = Function::New(env, ThrowTypeError);
exports["throwRangeError"] = Function::New(env, ThrowRangeError);
exports["catchError"] = Function::New(env, CatchError);
exports["catchErrorMessage"] = Function::New(env, CatchErrorMessage);
exports["doNotCatch"] = Function::New(env, DoNotCatch);
exports["catchAndRethrowError"] = Function::New(env, CatchAndRethrowError);
exports["throwErrorThatEscapesScope"] = Function::New(env, ThrowErrorThatEscapesScope);
exports["catchAndRethrowErrorThatEscapesScope"] =
Function::New(env, CatchAndRethrowErrorThatEscapesScope);
exports["throwFatalError"] = Function::New(env, ThrowFatalError);
return exports;
}