Skip to content

Commit 4ea22b8

Browse files
authored
chore: replace process.exit calls with actual error thrown (#1089)
1 parent e084f7a commit 4ea22b8

2 files changed

Lines changed: 18 additions & 61 deletions

File tree

lib/security_blog.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ export default class SecurityBlog extends SecurityRelease {
2525
const content = this.readVulnerabilitiesJSON();
2626
// validate the release date read from vulnerabilities JSON
2727
if (!content.releaseDate) {
28-
cli.error('Release date is not set in vulnerabilities.json,' +
28+
throw new Error('Release date is not set in vulnerabilities.json,' +
2929
' run `git node security --update-date=YYYY/MM/DD` to set the release date.');
30-
process.exit(1);
3130
}
3231

3332
validateDate(content.releaseDate);
@@ -109,9 +108,8 @@ export default class SecurityBlog extends SecurityRelease {
109108
// read vulnerabilities JSON file
110109
const content = this.readVulnerabilitiesJSON();
111110
if (!content.releaseDate) {
112-
cli.error('Release date is not set in vulnerabilities.json,' +
111+
throw new Error('Release date is not set in vulnerabilities.json,' +
113112
' run `git node security --update-date=YYYY/MM/DD` to set the release date.');
114-
process.exit(1);
115113
}
116114

117115
validateDate(content.releaseDate);
@@ -133,8 +131,7 @@ export default class SecurityBlog extends SecurityRelease {
133131
const preReleasePath = path.resolve(pathToBlogPosts, data.slug + '.md');
134132
let preReleaseContent = this.findExistingPreRelease(preReleasePath);
135133
if (!preReleaseContent) {
136-
cli.error(`Existing pre-release not found! Path: ${preReleasePath} `);
137-
process.exit(1);
134+
throw new Error(`Existing pre-release not found! Path: ${preReleasePath} `);
138135
}
139136

140137
const postReleaseContent = await this.buildPostRelease(template, data, content);
@@ -247,23 +244,20 @@ export default class SecurityBlog extends SecurityRelease {
247244
for (const report of reports) {
248245
const cveId = report.cveIds?.join(', ');
249246
if (!cveId) {
250-
this.cli.error(`CVE ID for vulnerability ${report.link} ${report.title} not found`);
251-
process.exit(1);
247+
throw new Error(`CVE ID for vulnerability ${report.link} ${report.title} not found`);
252248
}
253249
template += `## ${report.title} (${cveId}) - (${report.severity.rating})\n\n`;
254250
if (!report.summary) {
255-
this.cli.error(`Summary missing for vulnerability ${report.link} ` +
251+
throw new Error(`Summary missing for vulnerability ${report.link} ` +
256252
`${report.title}. Please create it before continuing.`);
257-
process.exit(1);
258253
}
259254

260255
template += `${report.summary}\n\n`;
261256
const releaseLines = report.affectedVersions.join(', ');
262257
template += `Impact:\n\n- This vulnerability affects all users\
263258
in active release lines: ${releaseLines}\n\n`;
264259
if (!report.patchAuthors) {
265-
this.cli.error(`Missing patch author for vulnerability ${report.link} ${report.title}`);
266-
process.exit(1);
260+
throw new Error(`Missing patch author for vulnerability ${report.link} ${report.title}`);
267261
}
268262
template += `Thank you, to ${report.reporter} for reporting this vulnerability\
269263
and thank you ${report.patchAuthors.join(' and ')} for fixing it.\n\n`;
@@ -324,8 +318,7 @@ export default class SecurityBlog extends SecurityRelease {
324318
const impact = new Map();
325319
for (const report of content.reports) {
326320
if (!report.severity?.rating) {
327-
this.cli.error(`severity.rating not found for report ${report.id}.`);
328-
process.exit(1);
321+
throw new Error(`severity.rating not found for report ${report.id}.`);
329322
}
330323

331324
for (const version of report.affectedVersions) {
@@ -348,8 +341,7 @@ export default class SecurityBlog extends SecurityRelease {
348341

349342
for (const report of content.reports) {
350343
if (!report.severity?.rating) {
351-
this.cli.error(`severity.rating not found for report ${report.id}.`);
352-
process.exit(1);
344+
throw new Error(`severity.rating not found for report ${report.id}.`);
353345
}
354346

355347
const rating = report.severity.rating;
@@ -367,8 +359,7 @@ export default class SecurityBlog extends SecurityRelease {
367359
getPreReleaseVulnerabilities(content) {
368360
for (const report of content.reports) {
369361
if (!report.severity?.rating) {
370-
this.cli.error(`severity.rating not found for report ${report.id}.`);
371-
process.exit(1);
362+
throw new Error(`severity.rating not found for report ${report.id}.`);
372363
}
373364
}
374365

test/unit/security_release.test.js

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,6 @@ import {
66
getHighestSeverityAnnouncement
77
} from '../../lib/security-release/security-release.js';
88

9-
const cli = {
10-
error() {}
11-
};
12-
13-
function assertExits(fn) {
14-
const originalExit = process.exit;
15-
process.exit = () => {
16-
throw new Error('process.exit');
17-
};
18-
19-
try {
20-
assert.throws(fn, /process\.exit/);
21-
} finally {
22-
process.exit = originalExit;
23-
}
24-
}
25-
269
function report(id, rating, affectedVersions = ['24.x']) {
2710
return {
2811
id,
@@ -110,7 +93,7 @@ describe('security_release: severity announcement', () => {
11093

11194
describe('security_blog: pre-release severity wording', () => {
11295
it('does not include severity counts in the summary', () => {
113-
const blog = new SecurityBlog(cli);
96+
const blog = new SecurityBlog();
11497
const content = {
11598
reports: [
11699
report(1, 'low'),
@@ -129,7 +112,7 @@ describe('security_blog: pre-release severity wording', () => {
129112
});
130113

131114
it('uses the highest severity per release line in impact text', () => {
132-
const blog = new SecurityBlog(cli);
115+
const blog = new SecurityBlog();
133116
const content = {
134117
reports: [
135118
report(1, 'low', ['22.x', '20.x']),
@@ -146,7 +129,7 @@ describe('security_blog: pre-release severity wording', () => {
146129
});
147130

148131
it('replaces the pre-release template placeholder with the highest severity sentence', () => {
149-
const blog = new SecurityBlog(cli);
132+
const blog = new SecurityBlog();
150133
const template = blog.getSecurityPreReleaseTemplate();
151134
const preRelease = blog.buildPreRelease(template, {
152135
annoucementDate: '2026-06-01T00:00:00.000Z',
@@ -170,12 +153,7 @@ describe('security_blog: pre-release severity wording', () => {
170153
});
171154

172155
it('exits when a report is missing a severity rating', () => {
173-
const errors = [];
174-
const blog = new SecurityBlog({
175-
error(message) {
176-
errors.push(message);
177-
}
178-
});
156+
const blog = new SecurityBlog();
179157
const content = {
180158
reports: [
181159
{
@@ -186,18 +164,14 @@ describe('security_blog: pre-release severity wording', () => {
186164
]
187165
};
188166

189-
assertExits(() => blog.getPreReleaseVulnerabilities(content));
190-
assertExits(() => blog.getImpact(content));
191-
assert.deepStrictEqual(errors, [
192-
'severity.rating not found for report 1.',
193-
'severity.rating not found for report 1.'
194-
]);
167+
assert.throws(() => blog.getPreReleaseVulnerabilities(content), /severity\.rating not found for report 1/);
168+
assert.throws(() => blog.getImpact(content), /severity\.rating not found for report 1/);
195169
});
196170
});
197171

198172
describe('security_blog: post-release severity wording', () => {
199173
it('keeps the vulnerability count list', () => {
200-
const blog = new SecurityBlog(cli);
174+
const blog = new SecurityBlog();
201175
const content = {
202176
reports: [
203177
report(1, 'low'),
@@ -213,12 +187,7 @@ describe('security_blog: post-release severity wording', () => {
213187
});
214188

215189
it('exits when a report is missing a severity rating', () => {
216-
const errors = [];
217-
const blog = new SecurityBlog({
218-
error(message) {
219-
errors.push(message);
220-
}
221-
});
190+
const blog = new SecurityBlog();
222191
const content = {
223192
reports: [
224193
{
@@ -229,9 +198,6 @@ describe('security_blog: post-release severity wording', () => {
229198
]
230199
};
231200

232-
assertExits(() => blog.getVulnerabilities(content));
233-
assert.deepStrictEqual(errors, [
234-
'severity.rating not found for report 1.'
235-
]);
201+
assert.throws(() => blog.getVulnerabilities(content), /severity\.rating not found for report 1/);
236202
});
237203
});

0 commit comments

Comments
 (0)