diff --git a/bin/moshcode.mjs b/bin/moshcode.mjs index 07c57cb..e2b17ae 100755 --- a/bin/moshcode.mjs +++ b/bin/moshcode.mjs @@ -23,7 +23,11 @@ const DEFAULT_SCRIPT = `while (alive) { function readScript(arg) { if (!arg || arg === "-") return fs.readFileSync(0, "utf8"); // stdin (paste) - return fs.readFileSync(arg, "utf8"); + try { + return fs.readFileSync(arg, "utf8"); + } catch (e) { + throw new Error(`moshcode run: cannot read script ${JSON.stringify(arg)} (${e.code || e.message})`); + } } function parseMax(value) { @@ -138,7 +142,13 @@ async function main() { } else file = a; } - const src = file ? readScript(file) : (fs.existsSync(EXAMPLE) ? fs.readFileSync(EXAMPLE, "utf8") : DEFAULT_SCRIPT); + let src; + try { + src = file ? readScript(file) : (fs.existsSync(EXAMPLE) ? fs.readFileSync(EXAMPLE, "utf8") : DEFAULT_SCRIPT); + } catch (e) { + console.error(String(e.message || e)); + process.exit(1); + } let ast; try { ast = compile(src); } catch (e) { console.error(String(e.message || e)); process.exit(1); } diff --git a/test/run-options.test.mjs b/test/run-options.test.mjs index 43ac876..954c299 100644 --- a/test/run-options.test.mjs +++ b/test/run-options.test.mjs @@ -33,3 +33,12 @@ test("run rejects multiple script files", () => { assert.equal(result.status, 1); assert.match(result.stderr, /moshcode run: expected one script file/); }); + +test("run reports missing script files without a stack trace", () => { + const missing = join(tmpdir(), "moshcode-missing-script.mosh"); + const result = run([missing, "--dry-run"]); + + assert.equal(result.status, 1); + assert.match(result.stderr, /moshcode run: cannot read script/); + assert.doesNotMatch(result.stderr, /Error: moshcode run/); +});