Skip to content

Commit

Permalink
Properly quit on uncaught exceptions (#4987)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 13, 2023
1 parent 71cd30a commit 896eb38
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cli/run/watch-cli.ts
Expand Up @@ -29,7 +29,7 @@ export async function watch(command: Record<string, any>): Promise<void> {
const runWatchHook = createWatchHooks(command);

onExit(close);
process.on('uncaughtException', close);
process.on('uncaughtException', closeWithError);
if (!process.stdin.isTTY) {
process.stdin.on('end', close);
process.stdin.resume();
Expand Down Expand Up @@ -147,7 +147,7 @@ export async function watch(command: Record<string, any>): Promise<void> {
}

async function close(code: number | null | undefined): Promise<void> {
process.removeListener('uncaughtException', close);
process.removeListener('uncaughtException', closeWithError);
// removing a non-existent listener is a no-op
process.stdin.removeListener('end', close);

Expand All @@ -160,3 +160,8 @@ export async function watch(command: Record<string, any>): Promise<void> {
// return a promise that never resolves to keep the process running
return new Promise(() => {});
}

function closeWithError(error: Error): void {
error.name = `Uncaught ${error.name}`;
handleError(error);
}
10 changes: 10 additions & 0 deletions test/cli/samples/handles-uncaught-errors/_config.js
@@ -0,0 +1,10 @@
const { assertIncludes } = require('../../../utils.js');

module.exports = defineTest({
description: 'handles uncaught errors',
command: 'rollup --config rollup.config.js',
error: () => true,
stderr(stderr) {
assertIncludes(stderr, 'TypeError: foo');
}
});
1 change: 1 addition & 0 deletions test/cli/samples/handles-uncaught-errors/main.js
@@ -0,0 +1 @@
assert.equal( 42, 42 );
16 changes: 16 additions & 0 deletions test/cli/samples/handles-uncaught-errors/rollup.config.js
@@ -0,0 +1,16 @@
module.exports = {
input: 'main.js',
output: {
format: 'cjs'
},
plugins: [
{
name: 'test',
buildStart() {
Promise.resolve().then(() => {
throw new TypeError('foo');
});
}
}
]
};

0 comments on commit 896eb38

Please sign in to comment.