diff --git a/cli/rt/02_console.js b/cli/rt/02_console.js index b5cab7617e..913ea44f29 100644 --- a/cli/rt/02_console.js +++ b/cli/rt/02_console.js @@ -762,29 +762,33 @@ ); } + const red = maybeColor(colors.red, inspectOptions); + for (const key of stringKeys) { - entries.push( - `${maybeQuoteString(key)}: ${ - inspectValueWithQuotes( - value[key], - ctx, - level + 1, - inspectOptions, - ) - }`, - ); + let propertyValue; + let error = null; + try { + propertyValue = value[key]; + } catch (error_) { + error = error_; + } + const inspectedValue = error == null + ? inspectValueWithQuotes(propertyValue, ctx, level + 1, inspectOptions) + : red(`[Thrown ${error.name}: ${error.message}]`); + entries.push(`${maybeQuoteString(key)}: ${inspectedValue}`); } for (const key of symbolKeys) { - entries.push( - `[${maybeQuoteSymbol(key)}]: ${ - inspectValueWithQuotes( - value[key], - ctx, - level + 1, - inspectOptions, - ) - }`, - ); + let propertyValue; + let error; + try { + propertyValue = value[key]; + } catch (error_) { + error = error_; + } + const inspectedValue = error == null + ? inspectValueWithQuotes(propertyValue, ctx, level + 1, inspectOptions) + : red(`Thrown ${error.name}: ${error.message}`); + entries.push(`[${maybeQuoteSymbol(key)}]: ${inspectedValue}`); } // Making sure color codes are ignored when calculating the total length const totalLength = entries.length + level + diff --git a/cli/tests/unit/console_test.ts b/cli/tests/unit/console_test.ts index 514b29cc28..c9cbcc313d 100644 --- a/cli/tests/unit/console_test.ts +++ b/cli/tests/unit/console_test.ts @@ -1498,6 +1498,17 @@ unitTest(function inspectString(): void { ); }); +unitTest(function inspectGetterError(): void { + assertEquals( + Deno.inspect({ + get foo() { + throw new Error("bar"); + }, + }), + "{ foo: [Thrown Error: bar] }", + ); +}); + unitTest(function inspectSorted(): void { assertEquals( stripColor(Deno.inspect({ b: 2, a: 1 }, { sorted: true })),