Skip to content

Commit

Permalink
fix(#301): Handle JSONStream.parse() errors more gracefully (#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnturner authored Jan 23, 2023
1 parent e038b0e commit 9f835b7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
4 changes: 2 additions & 2 deletions lib/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function getAuditor(
}
}

function audit(
async function audit(
config: AuditCiConfig,
reporter?: (summary: Summary, config: AuditCiConfig) => Summary
) {
Expand Down Expand Up @@ -70,7 +70,7 @@ function audit(
}
}

return run();
return await run();
}

export default audit;
37 changes: 30 additions & 7 deletions lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,29 @@ export function runProgram(
) {
const transform = new ReadlineTransform({ skipEmpty: true });
const proc = spawn(command, arguments_, options);
let recentMessage: string;
let errorMessage: string;
proc.stdout.setEncoding("utf8");
proc.stdout
.pipe(transform)
// TODO: Review this JSONStream.parse()
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
.pipe(JSONStream.parse())
.pipe(
transform.on("error", (error) => {
throw error;
})
)
.pipe(
eventStream.mapSync((data: string) => {
recentMessage = data;
return data;
})
)
.pipe(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore -- JSONStream.parse() accepts (pattern: any) when it should accept (pattern?: any)
JSONStream.parse().on("error", () => {
errorMessage = recentMessage;
throw new Error(errorMessage);
})
)
.pipe(
eventStream.mapSync((data: unknown) => {
if (!data) return;
Expand All @@ -167,8 +183,15 @@ export function runProgram(
})
);
return new Promise<void>((resolve, reject) => {
proc.on("close", () => resolve());
proc.on("error", (error) => reject(error));
proc.on("close", () => {
if (errorMessage) {
return reject(new Error(errorMessage));
}
return resolve();
});
proc.on("error", (error) =>
reject(errorMessage ? new Error(errorMessage) : error)
);
});
}

Expand Down

0 comments on commit 9f835b7

Please sign in to comment.