forked from nodejs/node-addon-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherror.js
More file actions
73 lines (59 loc) · 2.27 KB
/
error.js
File metadata and controls
73 lines (59 loc) · 2.27 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
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');
if (process.argv[2] === 'fatal') {
const binding = require(process.argv[3]);
binding.error.throwFatalError();
return;
}
test(`./build/${buildType}/binding.node`);
test(`./build/${buildType}/binding_noexcept.node`);
function test(bindingPath) {
const binding = require(bindingPath);
assert.throws(() => binding.error.throwApiError('test'), function(err) {
return err instanceof Error && err.message.includes('Invalid');
});
assert.throws(() => binding.error.throwJSError('test'), function(err) {
return err instanceof Error && err.message === 'test';
});
assert.throws(() => binding.error.throwTypeError('test'), function(err) {
return err instanceof TypeError && err.message === 'test';
});
assert.throws(() => binding.error.throwRangeError('test'), function(err) {
return err instanceof RangeError && err.message === 'test';
});
assert.throws(
() => binding.error.doNotCatch(
() => {
throw new TypeError('test');
}),
function(err) {
return err instanceof TypeError && err.message === 'test' && !err.caught;
});
assert.throws(
() => binding.error.catchAndRethrowError(
() => {
throw new TypeError('test');
}),
function(err) {
return err instanceof TypeError && err.message === 'test' && err.caught;
});
const err = binding.error.catchError(
() => { throw new TypeError('test'); });
assert(err instanceof TypeError);
assert.strictEqual(err.message, 'test');
const msg = binding.error.catchErrorMessage(
() => { throw new TypeError('test'); });
assert.strictEqual(msg, 'test');
assert.throws(() => binding.error.throwErrorThatEscapesScope('test'), function(err) {
return err instanceof Error && err.message === 'test';
});
assert.throws(() => binding.error.catchAndRethrowErrorThatEscapesScope('test'), function(err) {
return err instanceof Error && err.message === 'test' && err.caught;
});
const p = require('./napi_child').spawnSync(
process.execPath, [ __filename, 'fatal', bindingPath ]);
assert.ifError(p.error);
assert.ok(p.stderr.toString().includes(
'FATAL ERROR: Error::ThrowFatalError This is a fatal error'));
}