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:
parent
e96e3f9eb0
commit
f0f26cedcc
@ -1532,33 +1532,6 @@ static inline napi_status set_error_code(napi_env env,
|
|||||||
RETURN_STATUS_IF_FALSE(env,
|
RETURN_STATUS_IF_FALSE(env,
|
||||||
set_maybe.FromMaybe(false),
|
set_maybe.FromMaybe(false),
|
||||||
napi_generic_failure);
|
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;
|
return napi_ok;
|
||||||
}
|
}
|
||||||
|
@ -118,18 +118,18 @@ error = test_error.createErrorCode();
|
|||||||
assert.ok(error instanceof Error, 'expected error to be an instance of Error');
|
assert.ok(error instanceof Error, 'expected error to be an instance of Error');
|
||||||
assert.strictEqual(error.code, 'ERR_TEST_CODE');
|
assert.strictEqual(error.code, 'ERR_TEST_CODE');
|
||||||
assert.strictEqual(error.message, 'Error [error]');
|
assert.strictEqual(error.message, 'Error [error]');
|
||||||
assert.strictEqual(error.name, 'Error [ERR_TEST_CODE]');
|
assert.strictEqual(error.name, 'Error');
|
||||||
|
|
||||||
error = test_error.createRangeErrorCode();
|
error = test_error.createRangeErrorCode();
|
||||||
assert.ok(error instanceof RangeError,
|
assert.ok(error instanceof RangeError,
|
||||||
'expected error to be an instance of RangeError');
|
'expected error to be an instance of RangeError');
|
||||||
assert.strictEqual(error.message, 'RangeError [range error]');
|
assert.strictEqual(error.message, 'RangeError [range error]');
|
||||||
assert.strictEqual(error.code, 'ERR_TEST_CODE');
|
assert.strictEqual(error.code, 'ERR_TEST_CODE');
|
||||||
assert.strictEqual(error.name, 'RangeError [ERR_TEST_CODE]');
|
assert.strictEqual(error.name, 'RangeError');
|
||||||
|
|
||||||
error = test_error.createTypeErrorCode();
|
error = test_error.createTypeErrorCode();
|
||||||
assert.ok(error instanceof TypeError,
|
assert.ok(error instanceof TypeError,
|
||||||
'expected error to be an instance of TypeError');
|
'expected error to be an instance of TypeError');
|
||||||
assert.strictEqual(error.message, 'TypeError [type error]');
|
assert.strictEqual(error.message, 'TypeError [type error]');
|
||||||
assert.strictEqual(error.code, 'ERR_TEST_CODE');
|
assert.strictEqual(error.code, 'ERR_TEST_CODE');
|
||||||
assert.strictEqual(error.name, 'TypeError [ERR_TEST_CODE]');
|
assert.strictEqual(error.name, 'TypeError');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user