From 0162a0f5bf240e0a8f74612c31e2d646beceb205 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 e4850c63aaffb3..f88dd09cb05447 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');