Skip to content

Commit

Permalink
n-api: remove code from error name
Browse files Browse the repository at this point in the history
This is a first step to align the n-api errors towards errors created
in JS. The stack still has to be updated to add the error code.

PR-URL: nodejs#26738
Fixes: nodejs#26669
Fixes: nodejs#20253
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
  • Loading branch information
BridgeAR committed Mar 23, 2019
1 parent e96e3f9 commit f0f26ce
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 30 deletions.
27 changes: 0 additions & 27 deletions src/js_native_api_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1532,33 +1532,6 @@ static inline napi_status set_error_code(napi_env env,
RETURN_STATUS_IF_FALSE(env,
set_maybe.FromMaybe(false),
napi_generic_failure);

// now update the name to be "name [code]" where name is the
// original name and code is the code associated with the Error
v8::Local<v8::String> name_string;
CHECK_NEW_FROM_UTF8(env, name_string, "");
v8::Local<v8::Name> name_key;
CHECK_NEW_FROM_UTF8(env, name_key, "name");

auto maybe_name = err_object->Get(context, name_key);
if (!maybe_name.IsEmpty()) {
v8::Local<v8::Value> name = maybe_name.ToLocalChecked();
if (name->IsString()) {
name_string =
v8::String::Concat(isolate, name_string, name.As<v8::String>());
}
}
name_string = v8::String::Concat(
isolate, name_string, NAPI_FIXED_ONE_BYTE_STRING(isolate, " ["));
name_string =
v8::String::Concat(isolate, name_string, code_value.As<v8::String>());
name_string = v8::String::Concat(
isolate, name_string, NAPI_FIXED_ONE_BYTE_STRING(isolate, "]"));

set_maybe = err_object->Set(context, name_key, name_string);
RETURN_STATUS_IF_FALSE(env,
set_maybe.FromMaybe(false),
napi_generic_failure);
}
return napi_ok;
}
Expand Down
6 changes: 3 additions & 3 deletions test/js-native-api/test_error/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,18 @@ error = test_error.createErrorCode();
assert.ok(error instanceof Error, 'expected error to be an instance of Error');
assert.strictEqual(error.code, 'ERR_TEST_CODE');
assert.strictEqual(error.message, 'Error [error]');
assert.strictEqual(error.name, 'Error [ERR_TEST_CODE]');
assert.strictEqual(error.name, 'Error');

error = test_error.createRangeErrorCode();
assert.ok(error instanceof RangeError,
'expected error to be an instance of RangeError');
assert.strictEqual(error.message, 'RangeError [range error]');
assert.strictEqual(error.code, 'ERR_TEST_CODE');
assert.strictEqual(error.name, 'RangeError [ERR_TEST_CODE]');
assert.strictEqual(error.name, 'RangeError');

error = test_error.createTypeErrorCode();
assert.ok(error instanceof TypeError,
'expected error to be an instance of TypeError');
assert.strictEqual(error.message, 'TypeError [type error]');
assert.strictEqual(error.code, 'ERR_TEST_CODE');
assert.strictEqual(error.name, 'TypeError [ERR_TEST_CODE]');
assert.strictEqual(error.name, 'TypeError');

0 comments on commit f0f26ce

Please sign in to comment.