UNPKG

7.4 kBMarkdownView Raw
1## Errors
2
3NAN includes helpers for creating, throwing and catching Errors as much of this functionality varies across the supported versions of V8 and must be abstracted.
4
5Note that an Error object is simply a specialized form of `v8::Value`.
6
7Also consult the V8 Embedders Guide section on [Exceptions](https://developers.google.com/v8/embed#exceptions) for more information.
8
9 - <a href="#api_nan_error"><b><code>Nan::Error()</code></b></a>
10 - <a href="#api_nan_range_error"><b><code>Nan::RangeError()</code></b></a>
11 - <a href="#api_nan_reference_error"><b><code>Nan::ReferenceError()</code></b></a>
12 - <a href="#api_nan_syntax_error"><b><code>Nan::SyntaxError()</code></b></a>
13 - <a href="#api_nan_type_error"><b><code>Nan::TypeError()</code></b></a>
14 - <a href="#api_nan_throw_error"><b><code>Nan::ThrowError()</code></b></a>
15 - <a href="#api_nan_throw_range_error"><b><code>Nan::ThrowRangeError()</code></b></a>
16 - <a href="#api_nan_throw_reference_error"><b><code>Nan::ThrowReferenceError()</code></b></a>
17 - <a href="#api_nan_throw_syntax_error"><b><code>Nan::ThrowSyntaxError()</code></b></a>
18 - <a href="#api_nan_throw_type_error"><b><code>Nan::ThrowTypeError()</code></b></a>
19 - <a href="#api_nan_fatal_exception"><b><code>Nan::FatalException()</code></b></a>
20 - <a href="#api_nan_errno_exception"><b><code>Nan::ErrnoException()</code></b></a>
21 - <a href="#api_nan_try_catch"><b><code>Nan::TryCatch</code></b></a>
22
23
24<a name="api_nan_error"></a>
25### Nan::Error()
26
27Create a new Error object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.3/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
28
29Note that an Error object is simply a specialized form of `v8::Value`.
30
31Signature:
32
33```c++
34v8::Local<v8::Value> Nan::Error(const char *msg);
35v8::Local<v8::Value> Nan::Error(v8::Local<v8::String> msg);
36```
37
38
39<a name="api_nan_range_error"></a>
40### Nan::RangeError()
41
42Create a new RangeError object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.3/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
43
44Note that an RangeError object is simply a specialized form of `v8::Value`.
45
46Signature:
47
48```c++
49v8::Local<v8::Value> Nan::RangeError(const char *msg);
50v8::Local<v8::Value> Nan::RangeError(v8::Local<v8::String> msg);
51```
52
53
54<a name="api_nan_reference_error"></a>
55### Nan::ReferenceError()
56
57Create a new ReferenceError object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.3/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
58
59Note that an ReferenceError object is simply a specialized form of `v8::Value`.
60
61Signature:
62
63```c++
64v8::Local<v8::Value> Nan::ReferenceError(const char *msg);
65v8::Local<v8::Value> Nan::ReferenceError(v8::Local<v8::String> msg);
66```
67
68
69<a name="api_nan_syntax_error"></a>
70### Nan::SyntaxError()
71
72Create a new SyntaxError object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.3/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
73
74Note that an SyntaxError object is simply a specialized form of `v8::Value`.
75
76Signature:
77
78```c++
79v8::Local<v8::Value> Nan::SyntaxError(const char *msg);
80v8::Local<v8::Value> Nan::SyntaxError(v8::Local<v8::String> msg);
81```
82
83
84<a name="api_nan_type_error"></a>
85### Nan::TypeError()
86
87Create a new TypeError object using the [v8::Exception](https://v8docs.nodesource.com/io.js-3.3/da/d6a/classv8_1_1_exception.html) class in a way that is compatible across the supported versions of V8.
88
89Note that an TypeError object is simply a specialized form of `v8::Value`.
90
91Signature:
92
93```c++
94v8::Local<v8::Value> Nan::TypeError(const char *msg);
95v8::Local<v8::Value> Nan::TypeError(v8::Local<v8::String> msg);
96```
97
98
99<a name="api_nan_throw_error"></a>
100### Nan::ThrowError()
101
102Throw an Error object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new Error object will be created.
103
104Signature:
105
106```c++
107void Nan::ThrowError(const char *msg);
108void Nan::ThrowError(v8::Local<v8::String> msg);
109void Nan::ThrowError(v8::Local<v8::Value> error);
110```
111
112
113<a name="api_nan_throw_range_error"></a>
114### Nan::ThrowRangeError()
115
116Throw an RangeError object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new RangeError object will be created.
117
118Signature:
119
120```c++
121void Nan::ThrowRangeError(const char *msg);
122void Nan::ThrowRangeError(v8::Local<v8::String> msg);
123void Nan::ThrowRangeError(v8::Local<v8::Value> error);
124```
125
126
127<a name="api_nan_throw_reference_error"></a>
128### Nan::ThrowReferenceError()
129
130Throw an ReferenceError object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new ReferenceError object will be created.
131
132Signature:
133
134```c++
135void Nan::ThrowReferenceError(const char *msg);
136void Nan::ThrowReferenceError(v8::Local<v8::String> msg);
137void Nan::ThrowReferenceError(v8::Local<v8::Value> error);
138```
139
140
141<a name="api_nan_throw_syntax_error"></a>
142### Nan::ThrowSyntaxError()
143
144Throw an SyntaxError object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new SyntaxError object will be created.
145
146Signature:
147
148```c++
149void Nan::ThrowSyntaxError(const char *msg);
150void Nan::ThrowSyntaxError(v8::Local<v8::String> msg);
151void Nan::ThrowSyntaxError(v8::Local<v8::Value> error);
152```
153
154
155<a name="api_nan_throw_type_error"></a>
156### Nan::ThrowTypeError()
157
158Throw an TypeError object (a specialized `v8::Value` as above) in the current context. If a `msg` is provided, a new TypeError object will be created.
159
160Signature:
161
162```c++
163void Nan::ThrowTypeError(const char *msg);
164void Nan::ThrowTypeError(v8::Local<v8::String> msg);
165void Nan::ThrowTypeError(v8::Local<v8::Value> error);
166```
167
168<a name="api_nan_fatal_exception"></a>
169### Nan::FatalException()
170
171Replaces `node::FatalException()` which has a different API across supported versions of Node. For use with [`Nan::TryCatch`](#api_nan_try_catch).
172
173Signature:
174
175```c++
176void Nan::FatalException(const Nan::TryCatch& try_catch);
177```
178
179<a name="api_nan_errno_exception"></a>
180### Nan::ErrnoException()
181
182Replaces `node::ErrnoException()` which has a different API across supported versions of Node.
183
184Signature:
185
186```c++
187v8::Local<v8::Value> Nan::ErrnoException(int errorno,
188 const char* syscall = NULL,
189 const char* message = NULL,
190 const char* path = NULL);
191```
192
193
194<a name="api_nan_try_catch"></a>
195### Nan::TryCatch
196
197A simple wrapper around [`v8::TryCatch`](https://v8docs.nodesource.com/io.js-3.3/d4/dc6/classv8_1_1_try_catch.html) compatible with all supported versions of V8. Can be used as a direct replacement in most cases. See also [`Nan::FatalException()`](#api_nan_fatal_exception) for an internal use compatible with `node::FatalException`.
198
199Signature:
200
201```c++
202class Nan::TryCatch {
203 public:
204 Nan::TryCatch();
205
206 bool HasCaught() const;
207
208 bool CanContinue() const;
209
210 v8::Local<v8::Value> ReThrow();
211
212 v8::Local<v8::Value> Exception() const;
213
214 // Nan::MaybeLocal for older versions of V8
215 v8::MaybeLocal<v8::Value> StackTrace() const;
216
217 v8::Local<v8::Message> Message() const;
218
219 void Reset();
220
221 void SetVerbose(bool value);
222
223 void SetCaptureMessage(bool value);
224};
225```
226