diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 83c254c3d6c464..e4489479fe99cd 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2529,6 +2529,11 @@ function formatProperty(ctx, value, recurseTimes, key, type, desc, let name, str; let extra = ' '; desc ??= ObjectGetOwnPropertyDescriptor(value, key); + // Find the property's descriptor by walking along the prototype chain. + while (desc === undefined) { + value = ObjectGetPrototypeOf(value); + desc = ObjectGetOwnPropertyDescriptor(value, key); + } if (desc.value !== undefined) { const diff = (ctx.compact !== true || type !== kObjectType) ? 2 : 3; ctx.indentationLvl += diff; diff --git a/test/parallel/test-util-inspect-format-property.js b/test/parallel/test-util-inspect-format-property.js new file mode 100644 index 00000000000000..551043ba7e14d1 --- /dev/null +++ b/test/parallel/test-util-inspect-format-property.js @@ -0,0 +1,23 @@ +'use strict'; + +require('../common'); + +const { inspect } = require('util'); + +class CustomError extends Error { + get cause() { + return this._cause; + } + get errors() { + return this._errors; + } + constructor(_cause, _errors) { + super(); + this._cause = _cause; + this._errors = _errors; + } +} + +// "cause" and "errors" properties should not +// cause to a error to be thrown while formatting +inspect(new CustomError('', []));