Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion scripts/source.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export function readJson(file, fallback = null) {
try {
return JSON.parse(fs.readFileSync(file, "utf8"));
} catch (error) {
throw new Error(`[clawsweeper-state] readJson: malformed JSON in ${file}`, { cause: error });
if (error instanceof SyntaxError) {
throw new Error(`[clawsweeper-state] readJson: malformed JSON in ${file}`, { cause: error });
}
throw error;
}
}

Expand Down
18 changes: 18 additions & 0 deletions test/source.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ test("readJson throws on malformed JSON without a fallback", () => {
fs.rmSync(dir, { recursive: true, force: true });
}
});

test("readJson does not wrap directory I/O failures as malformed JSON", () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "clawsweeper-source-"));
try {
const file = path.join(dir, "not-a-file.json");
fs.mkdirSync(file);
assert.throws(
() => readJson(file),
(error) => {
assert.doesNotMatch(String(error.message), /malformed JSON/);
assert.match(String(error.code ?? error.cause?.code ?? ""), /EISDIR|EPERM/);
return true;
},
);
} finally {
fs.rmSync(dir, { recursive: true, force: true });
}
});
Loading