diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index cf0a8892f8f4..622dab4eae8a 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -2545,8 +2545,11 @@ function formatPromise(ctx, value, recurseTimes) { } function formatExtraProperties(ctx, value, recurseTimes, key, typedArray) { + // Read the value before indenting: the getter throws for a detached + // DataView, and formatRaw() then retries with a fallback value. + const extraValue = value[key]; ctx.indentationLvl += 2; - const str = formatValue(ctx, value[key], recurseTimes, typedArray); + const str = formatValue(ctx, extraValue, recurseTimes, typedArray); ctx.indentationLvl -= 2; // These entries are mainly getters. Should they be formatted like getters? diff --git a/test/parallel/test-util-inspect.js b/test/parallel/test-util-inspect.js index 3fc17bc6ae91..4e26661a4b49 100644 --- a/test/parallel/test-util-inspect.js +++ b/test/parallel/test-util-inspect.js @@ -217,10 +217,10 @@ assert.doesNotMatch( assert.strictEqual( util.inspect(dv), 'DataView {\n' + - ' [byteLength]: 0,\n' + - ' [byteOffset]: undefined,\n' + - ' [buffer]: ArrayBuffer { (detached), [byteLength]: 0 }\n' + - ' }', + ' [byteLength]: 0,\n' + + ' [byteOffset]: undefined,\n' + + ' [buffer]: ArrayBuffer { (detached), [byteLength]: 0 }\n' + + '}', ); }