From 5260f53e55515354b2b80f7c282837d32f84aa3b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sun, 11 Jun 2023 10:10:47 +0200 Subject: [PATCH] lib: add support for inherited custom inspection methods PR-URL: https://github.com/nodejs/node/pull/48306 Fixes: https://github.com/nodejs/node/issues/48207 Reviewed-By: James M Snell --- lib/internal/error_serdes.js | 4 +--- test/parallel/test-error-serdes.js | 8 ++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/internal/error_serdes.js b/lib/internal/error_serdes.js index 5c25830600237c..d340c4a4bbc7fd 100644 --- a/lib/internal/error_serdes.js +++ b/lib/internal/error_serdes.js @@ -13,7 +13,6 @@ const { ObjectGetOwnPropertyNames, ObjectGetPrototypeOf, ObjectKeys, - ObjectPrototypeHasOwnProperty, ObjectPrototypeToString, RangeError, ReferenceError, @@ -134,8 +133,7 @@ function serializeError(error) { // Continue regardless of error. } try { - if (error != null && - ObjectPrototypeHasOwnProperty(error, customInspectSymbol)) { + if (error != null && customInspectSymbol in error) { return Buffer.from(StringFromCharCode(kCustomInspectedObject) + inspect(error), 'utf8'); } } catch { diff --git a/test/parallel/test-error-serdes.js b/test/parallel/test-error-serdes.js index 3b61d3a4b9b34b..da09e6ed0a4d9f 100644 --- a/test/parallel/test-error-serdes.js +++ b/test/parallel/test-error-serdes.js @@ -125,3 +125,11 @@ const data = { } }; assert.strictEqual(inspect(cycle(data)), 'barbaz'); + +const inheritedCustomInspect = new class { + foo = 'bar'; + [inspect.custom]() { + return 'barbaz'; + } +}(); +assert.strictEqual(inspect(cycle(inheritedCustomInspect)), 'barbaz');