deno: Errors in dynamic imports are not properly caught

Runtime

debian 10
deno 1.6.2
v8 8.8.278.2
typescript 4.1.3

//a.ts
import("./b.ts").catch(e=>{
  console.log("caught import b.ts error")
  console.error(e)
})

import("./c.ts").catch(e=>{
  console.log("caught import c.ts error")
  console.error(e)
})

setInterval(function () {
  import("./c.ts").catch((e) => {
    console.log("caught interval import c.ts error");
    console.error(e);
  });
}, 1e3);
//b.ts
// @ts-ignore
import "./b1.ts";
export default () => "error";
//c.ts
// @ts-ignore
await import("./c1.ts");
export default () => "error";

create those files and then run deno run -A a.ts, you will get log like below:

caught import b.ts error
TypeError: Cannot resolve module "file://yourdir/b1.ts" from "file://yourdir/b.ts".
    at file://yourdir/b.ts:3:0
caught import c.ts error
TypeError: Cannot resolve module "file://yourdir/c1.ts".
error: Uncaught TypeError: Cannot resolve module "file://yourdir/c1.ts".

import('./c.ts') first error is caught, but then second import is not caught and it make my program exit

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Smaller repro:

try {
  await import(`data:application/typescript;base64,${btoa(`await new Promise(r => setTimeout(r, 100)); throw new Error("foo");`)}`);
  // The rejection causes an exception before being caught here.
} catch (error) {
  console.trace(error, "--");
}

It occurs because this line: https://github.com/denoland/deno/blob/5873adeb5e6ec2113eeb5adc964b7ce129d4905d/core/runtime.rs#L814 attempts to remove the promise from the list of exceptions before it is ever added by this line: https://github.com/denoland/deno/blob/5873adeb5e6ec2113eeb5adc964b7ce129d4905d/core/bindings.rs#L284-L286 so it is never removed. For modules with “truly” async top levels, the first one is run after the promise is returned while the second is run after it rejects. Fix coming.

Some of the fixes that are out there may impact this @bartlomieju.