Skip to content

Commit b5ad730

Browse files
kibertoadjaylinski
authored andcommitted
Improve error handling
1 parent ff02463 commit b5ad730

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

bin/handlebars.mjs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ Precompiler.loadTemplates(argv, function (err, opts) {
125125
if (opts.help || (!opts.templates.length && !opts.version)) {
126126
parser.showHelp('log');
127127
} else {
128-
Precompiler.cli(opts);
128+
// cli() is async (returns a Promise), so errors would become unhandled
129+
// rejections. Re-throw via nextTick to surface them as uncaught exceptions.
130+
Promise.resolve(Precompiler.cli(opts)).catch((error) => {
131+
process.nextTick(() => {
132+
throw error;
133+
});
134+
});
129135
}
130136
});

tasks/tests/cli.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const fs = require('fs');
2+
const { exec } = require('child_process');
23
const { execCommand, FileTestHelper } = require('cli-testlab');
34
const Handlebars = require('../../lib');
45

@@ -245,6 +246,21 @@ describe('bin/handlebars', function () {
245246
});
246247
});
247248

249+
describe('error handling', function () {
250+
it('should not produce unhandled promise rejections on async errors', async function () {
251+
const { stderr } = await new Promise((resolve) => {
252+
exec(
253+
`node --unhandled-rejections=warn ./bin/handlebars.mjs -i "<div>test</div>" -N test --map /nonexistent/dir/test.map`,
254+
(error, stdout, stderr) => {
255+
resolve({ exitCode: error ? error.code : 0, stderr });
256+
}
257+
);
258+
});
259+
260+
expect(stderr).not.toMatch(/unhandled/i);
261+
});
262+
});
263+
248264
describe('negated boolean flags', function () {
249265
it('--no-amd negates --amd (issue #1673)', async function () {
250266
const result = await execCommand(

0 commit comments

Comments
 (0)