Skip to content

Commit 9f835b7

Browse files
authored
fix(#301): Handle JSONStream.parse() errors more gracefully (#306)
1 parent e038b0e commit 9f835b7

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/audit.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function getAuditor(
3030
}
3131
}
3232

33-
function audit(
33+
async function audit(
3434
config: AuditCiConfig,
3535
reporter?: (summary: Summary, config: AuditCiConfig) => Summary
3636
) {
@@ -70,7 +70,7 @@ function audit(
7070
}
7171
}
7272

73-
return run();
73+
return await run();
7474
}
7575

7676
export default audit;

lib/common.ts

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,29 @@ export function runProgram(
134134
) {
135135
const transform = new ReadlineTransform({ skipEmpty: true });
136136
const proc = spawn(command, arguments_, options);
137+
let recentMessage: string;
138+
let errorMessage: string;
137139
proc.stdout.setEncoding("utf8");
138140
proc.stdout
139-
.pipe(transform)
140-
// TODO: Review this JSONStream.parse()
141-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
142-
// @ts-ignore
143-
.pipe(JSONStream.parse())
141+
.pipe(
142+
transform.on("error", (error) => {
143+
throw error;
144+
})
145+
)
146+
.pipe(
147+
eventStream.mapSync((data: string) => {
148+
recentMessage = data;
149+
return data;
150+
})
151+
)
152+
.pipe(
153+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
154+
// @ts-ignore -- JSONStream.parse() accepts (pattern: any) when it should accept (pattern?: any)
155+
JSONStream.parse().on("error", () => {
156+
errorMessage = recentMessage;
157+
throw new Error(errorMessage);
158+
})
159+
)
144160
.pipe(
145161
eventStream.mapSync((data: unknown) => {
146162
if (!data) return;
@@ -167,8 +183,15 @@ export function runProgram(
167183
})
168184
);
169185
return new Promise<void>((resolve, reject) => {
170-
proc.on("close", () => resolve());
171-
proc.on("error", (error) => reject(error));
186+
proc.on("close", () => {
187+
if (errorMessage) {
188+
return reject(new Error(errorMessage));
189+
}
190+
return resolve();
191+
});
192+
proc.on("error", (error) =>
193+
reject(errorMessage ? new Error(errorMessage) : error)
194+
);
172195
});
173196
}
174197

0 commit comments

Comments
 (0)