Improve error handling

This commit is contained in:
Igor Savin
2026-03-19 01:26:22 +02:00
committed by Jay Linski
parent ff02463429
commit b5ad730473
2 changed files with 23 additions and 1 deletions
+7 -1
View File
@@ -125,6 +125,12 @@ Precompiler.loadTemplates(argv, function (err, opts) {
if (opts.help || (!opts.templates.length && !opts.version)) { if (opts.help || (!opts.templates.length && !opts.version)) {
parser.showHelp('log'); parser.showHelp('log');
} else { } else {
Precompiler.cli(opts); // cli() is async (returns a Promise), so errors would become unhandled
// rejections. Re-throw via nextTick to surface them as uncaught exceptions.
Promise.resolve(Precompiler.cli(opts)).catch((error) => {
process.nextTick(() => {
throw error;
});
});
} }
}); });
+16
View File
@@ -1,4 +1,5 @@
const fs = require('fs'); const fs = require('fs');
const { exec } = require('child_process');
const { execCommand, FileTestHelper } = require('cli-testlab'); const { execCommand, FileTestHelper } = require('cli-testlab');
const Handlebars = require('../../lib'); const Handlebars = require('../../lib');
@@ -245,6 +246,21 @@ describe('bin/handlebars', function () {
}); });
}); });
describe('error handling', function () {
it('should not produce unhandled promise rejections on async errors', async function () {
const { stderr } = await new Promise((resolve) => {
exec(
`node --unhandled-rejections=warn ./bin/handlebars.mjs -i "<div>test</div>" -N test --map /nonexistent/dir/test.map`,
(error, stdout, stderr) => {
resolve({ exitCode: error ? error.code : 0, stderr });
}
);
});
expect(stderr).not.toMatch(/unhandled/i);
});
});
describe('negated boolean flags', function () { describe('negated boolean flags', function () {
it('--no-amd negates --amd (issue #1673)', async function () { it('--no-amd negates --amd (issue #1673)', async function () {
const result = await execCommand( const result = await execCommand(