forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nodejs#147 from boingoing/add_napi_is_error
napi: Add napi_is_error API to identify NativeError objects
- Loading branch information
Showing
5 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"targets": [ | ||
{ | ||
"target_name": "test_error", | ||
"sources": [ "test_error.cc" ] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const common = require('../../common'); | ||
const test_error = require(`./build/${common.buildType}/test_error`); | ||
const assert = require('assert'); | ||
const theError = new Error('Some error'); | ||
const theTypeError = new TypeError('Some type error'); | ||
const theSyntaxError = new SyntaxError('Some syntax error'); | ||
const theRangeError = new RangeError('Some type error'); | ||
const theReferenceError = new ReferenceError('Some reference error'); | ||
const theURIError = new URIError('Some URI error'); | ||
const theEvalError = new EvalError('Some eval error'); | ||
|
||
class MyError extends Error { } | ||
const myError = new MyError('Some MyError'); | ||
|
||
// Test that native error object is correctly classed | ||
assert.strictEqual(test_error.checkError(theError), true, | ||
'Error object correctly classed by napi_is_error'); | ||
|
||
// Test that native type error object is correctly classed | ||
assert.strictEqual(test_error.checkError(theTypeError), true, | ||
'Type error object correctly classed by napi_is_error'); | ||
|
||
// Test that native syntax error object is correctly classed | ||
assert.strictEqual(test_error.checkError(theSyntaxError), true, | ||
'Syntax error object correctly classed by napi_is_error'); | ||
|
||
// Test that native range error object is correctly classed | ||
assert.strictEqual(test_error.checkError(theRangeError), true, | ||
'Range error object correctly classed by napi_is_error'); | ||
|
||
// Test that native URI error object is correctly classed | ||
assert.strictEqual(test_error.checkError(theURIError), true, | ||
'URI error object correctly classed by napi_is_error'); | ||
|
||
// Test that native eval error object is correctly classed | ||
assert.strictEqual(test_error.checkError(theEvalError), true, | ||
'Eval error object correctly classed by napi_is_error'); | ||
|
||
// Test that class derived from native error is correctly classed | ||
assert.strictEqual(test_error.checkError(myError), true, | ||
'Class derived from native error correctly classed by napi_is_error'); | ||
|
||
// Test that non-error object is correctly classed | ||
assert.strictEqual(test_error.checkError({}), false, | ||
'Non-error object correctly classed by napi_is_error'); | ||
|
||
// Test that non-error primitive is correctly classed | ||
assert.strictEqual(test_error.checkError('non-object'), false, | ||
'Non-error primitive correctly classed by napi_is_error'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <node_jsvmapi.h> | ||
|
||
void checkError(napi_env e, napi_callback_info info) { | ||
napi_status status; | ||
napi_value jsError; | ||
|
||
status = napi_get_cb_args(e, info, &jsError, 1); | ||
if (status != napi_ok) return; | ||
|
||
bool r; | ||
status = napi_is_error(e, jsError, &r); | ||
if (status != napi_ok) return; | ||
|
||
napi_value result; | ||
if (r) { | ||
status = napi_get_true(e, &result); | ||
} else { | ||
status = napi_get_false(e, &result); | ||
} | ||
if (status != napi_ok) return; | ||
|
||
status = napi_set_return_value(e, info, result); | ||
if (status != napi_ok) return; | ||
} | ||
|
||
void Init(napi_env env, napi_value exports, napi_value module) { | ||
napi_status status; | ||
|
||
napi_property_descriptor descriptors[] = { | ||
{ "checkError", checkError } | ||
}; | ||
|
||
status = napi_define_properties( | ||
env, exports, sizeof(descriptors) / sizeof(*descriptors), descriptors); | ||
if (status != napi_ok) return; | ||
} | ||
|
||
NODE_MODULE_ABI(addon, Init) |