From f0f26cedccdb8e5d891bff2949651ea8c3a839c1 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 18 Mar 2019 15:43:11 +0100 Subject: [PATCH] n-api: remove code from error name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Matteo Collina Reviewed-By: Michaƫl Zasso Reviewed-By: Joyee Cheung --- src/js_native_api_v8.cc | 27 --------------------------- test/js-native-api/test_error/test.js | 6 +++--- 2 files changed, 3 insertions(+), 30 deletions(-) diff --git a/src/js_native_api_v8.cc b/src/js_native_api_v8.cc index 97600a9fb15f58..f7c6b6db4ffebc 100644 --- a/src/js_native_api_v8.cc +++ b/src/js_native_api_v8.cc @@ -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 name_string; - CHECK_NEW_FROM_UTF8(env, name_string, ""); - v8::Local 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 name = maybe_name.ToLocalChecked(); - if (name->IsString()) { - name_string = - v8::String::Concat(isolate, name_string, name.As()); - } - } - 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()); - 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; } diff --git a/test/js-native-api/test_error/test.js b/test/js-native-api/test_error/test.js index d4b1d8a971ee09..e7e0ded476fe43 100644 --- a/test/js-native-api/test_error/test.js +++ b/test/js-native-api/test_error/test.js @@ -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');