1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-03 12:58:54 -05:00

fix(cli/console): Catch and format getter errors (#7766)

This commit is contained in:
Nayeem Rahman 2020-10-01 10:25:34 +01:00 committed by GitHub
parent 290da280a8
commit b689e60b60
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 20 deletions

View file

@ -762,29 +762,33 @@
); );
} }
const red = maybeColor(colors.red, inspectOptions);
for (const key of stringKeys) { for (const key of stringKeys) {
entries.push( let propertyValue;
`${maybeQuoteString(key)}: ${ let error = null;
inspectValueWithQuotes( try {
value[key], propertyValue = value[key];
ctx, } catch (error_) {
level + 1, error = error_;
inspectOptions, }
) 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) { for (const key of symbolKeys) {
entries.push( let propertyValue;
`[${maybeQuoteSymbol(key)}]: ${ let error;
inspectValueWithQuotes( try {
value[key], propertyValue = value[key];
ctx, } catch (error_) {
level + 1, error = error_;
inspectOptions, }
) 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 // Making sure color codes are ignored when calculating the total length
const totalLength = entries.length + level + const totalLength = entries.length + level +

View file

@ -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 { unitTest(function inspectSorted(): void {
assertEquals( assertEquals(
stripColor(Deno.inspect({ b: 2, a: 1 }, { sorted: true })), stripColor(Deno.inspect({ b: 2, a: 1 }, { sorted: true })),