0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

n-api: remove code from error name

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: https://github.com/nodejs/node/pull/26738
Fixes: https://github.com/nodejs/node/issues/26669
Fixes: https://github.com/nodejs/node/issues/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>
This commit is contained in:
Ruben Bridgewater 2019-03-18 15:43:11 +01:00
parent e96e3f9eb0
commit f0f26cedcc
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 3 additions and 30 deletions

View File

@ -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;
}

View File

@ -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');