1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-07 06:46:59 -05:00

fix(console): handle error when inspecting promise-like (#19083)

Fixes
https://discord.com/channels/684898665143206084/684911491035430919/1105900195406958672.

This was caused by: 
- A `TypeError` from `core.getPromiseDetails()` for promise-likes which
also lead to that code path.
- Swallowing internal formatting errors by returning `undefined`. I've
made it so that a special message is formatted in that case instead
(note that this case is fixed now):
![image](https://github.com/denoland/deno/assets/29990554/65bb9612-60b2-4e31-bf5e-e20976601593)
This commit is contained in:
Nayeem Rahman 2023-05-11 14:08:17 +01:00 committed by David Sherret
parent 89d0f0c4e3
commit 14bf8dc506
2 changed files with 21 additions and 29 deletions

View file

@ -2235,6 +2235,13 @@ Deno.test(function inspectWithPrototypePollution() {
} }
}); });
Deno.test(function inspectPromiseLike() {
assertEquals(
Deno.inspect(Object.create(Promise.prototype)),
"Promise { <unknown> }",
);
});
Deno.test(function inspectorMethods() { Deno.test(function inspectorMethods() {
console.timeStamp("test"); console.timeStamp("test");
console.profile("test"); console.profile("test");

View file

@ -161,6 +161,7 @@ const styles = {
// TODO(BridgeAR): Highlight regular expressions properly. // TODO(BridgeAR): Highlight regular expressions properly.
regexp: "red", regexp: "red",
module: "underline", module: "underline",
internalError: "red",
}; };
const defaultFG = 39; const defaultFG = 39;
@ -1022,7 +1023,6 @@ function formatRaw(ctx, value, recurseTimes, typedArray, proxyDetails) {
ArrayPrototypePush(ctx.seen, value); ArrayPrototypePush(ctx.seen, value);
ctx.currentDepth = recurseTimes; ctx.currentDepth = recurseTimes;
let output; let output;
const indentationLvl = ctx.indentationLvl;
try { try {
output = formatter(ctx, value, recurseTimes); output = formatter(ctx, value, recurseTimes);
for (i = 0; i < keys.length; i++) { for (i = 0; i < keys.length; i++) {
@ -1034,13 +1034,12 @@ function formatRaw(ctx, value, recurseTimes, typedArray, proxyDetails) {
if (protoProps !== undefined) { if (protoProps !== undefined) {
ArrayPrototypePushApply(output, protoProps); ArrayPrototypePushApply(output, protoProps);
} }
} catch (err) { } catch (error) {
const constructorName = StringPrototypeSlice( // TODO(wafuwafu13): Implement stack overflow check
getCtxStyle(value, constructor, tag), return ctx.stylize(
0, `[Internal Formatting Error] ${error.stack}`,
-1, "internalError",
); );
return handleMaxCallStackSize(ctx, err, constructorName, indentationLvl);
} }
if (ctx.circular !== undefined) { if (ctx.circular !== undefined) {
@ -1658,8 +1657,14 @@ const PromiseState = {
function formatPromise(ctx, value, recurseTimes) { function formatPromise(ctx, value, recurseTimes) {
let output; let output;
// TODO(wafuwafu13): Implement let opResult;
const { 0: state, 1: result } = core.getPromiseDetails(value); // This op will fail for non-promises, but we get here for some promise-likes.
try {
opResult = core.getPromiseDetails(value);
} catch {
return [ctx.stylize("<unknown>", "special")];
}
const { 0: state, 1: result } = opResult;
if (state === PromiseState.Pending) { if (state === PromiseState.Pending) {
output = [ctx.stylize("<pending>", "special")]; output = [ctx.stylize("<pending>", "special")];
} else { } else {
@ -1770,26 +1775,6 @@ function formatProperty(
return `${name}:${extra}${str}`; return `${name}:${extra}${str}`;
} }
function handleMaxCallStackSize(
_ctx,
_err,
_constructorName,
_indentationLvl,
) {
// TODO(wafuwafu13): Implement
// if (isStackOverflowError(err)) {
// ctx.seen.pop();
// ctx.indentationLvl = indentationLvl;
// return ctx.stylize(
// `[${constructorName}: Inspection interrupted ` +
// 'prematurely. Maximum call stack size exceeded.]',
// 'special'
// );
// }
// /* c8 ignore next */
// assert.fail(err.stack);
}
const colorRegExp = new SafeRegExp("\u001b\\[\\d\\d?m", "g"); const colorRegExp = new SafeRegExp("\u001b\\[\\d\\d?m", "g");
function removeColors(str) { function removeColors(str) {
return StringPrototypeReplace(str, colorRegExp, ""); return StringPrototypeReplace(str, colorRegExp, "");