Wrap JSON parse errors in readSarifFile with context

This commit is contained in:
Michael B. Gale
2026-09-22 18:53:03 +01:00
parent 8e36092a16
commit fe0a932a6a
2 changed files with 17 additions and 4 deletions

View File

@@ -3,6 +3,7 @@ import * as fs from "fs";
import { Logger } from "../logging";
import * as sarif from "sarif";
import { getErrorMessage } from "../util";
export type * from "sarif";
@@ -48,7 +49,13 @@ export function getToolNames(sarifFile: Partial<sarif.Log>): string[] {
* @returns The resulting JSON value, cast to a SARIF `Log`.
*/
export function readSarifFile(sarifFilePath: string): Partial<sarif.Log> {
return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as sarif.Log;
try {
return JSON.parse(fs.readFileSync(sarifFilePath, "utf8")) as sarif.Log;
} catch (err) {
throw new Error(
`Parsing SARIF file at '${sarifFilePath}' failed: ${getErrorMessage(err)}`,
);
}
}
// Takes a list of paths to sarif files and combines them together,