Skip to content

Commit cf27a82

Browse files
committed
fix(errors): handle changelog file not found
- Added ChangelogFileNotFoundError - Improved error handling in changelog service - Added unit test for file not found error
1 parent 9bca7ed commit cf27a82

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

src/errors.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,12 @@ export class InvalidManifestError extends CustomError {
4343
super(message, 'InvalidManifestError');
4444
}
4545
}
46+
47+
/**
48+
* Error thrown when the changelog file is not found.
49+
*/
50+
export class ChangelogFileNotFoundError extends CustomError {
51+
constructor(message: string) {
52+
super(message, 'ChangelogFileNotFoundError');
53+
}
54+
}

src/services/changelogService.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FileHandler } from '../utils/fileHandler';
22
import { GitService } from './gitService';
33
import * as core from '@actions/core';
4+
import { FileNotFoundError } from '../errors';
45

56
/**
67
* Service for managing the changelog file.
@@ -50,7 +51,18 @@ export class ChangelogService {
5051
*/
5152
async updateChangelog(changelogContent: string): Promise<void> {
5253
const changelogPath = this._getChangelogPath();
53-
const existingChangelog = await this.fileHandler.readFile(changelogPath);
54+
let existingChangelog: string;
55+
try {
56+
existingChangelog = await this.fileHandler.readFile(changelogPath);
57+
} catch (error: any) {
58+
if (error.code === 'ENOENT') {
59+
throw new FileNotFoundError(
60+
`Changelog file not found at ${changelogPath}`,
61+
);
62+
}
63+
throw error;
64+
}
65+
5466
const versionMatch = changelogContent.match(/## v(\d+\.\d+\.\d+)/);
5567
if (versionMatch) {
5668
const newVersion = versionMatch[0];

tests/services/changelogService.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ChangelogService } from '../../src/services/changelogService';
33
import { FileHandler } from '../../src/utils/fileHandler';
44
import { GitService } from '../../src/services/gitService';
55
import * as core from '@actions/core';
6+
import { FileNotFoundError } from '../../src/errors';
67

78
vi.mock('@actions/exec');
89
vi.mock('../../src/utils/fileHandler');
@@ -65,4 +66,15 @@ describe('ChangelogService', () => {
6566
'Changelog for version ## v1.1.0 already exists. Skipping.',
6667
);
6768
});
69+
70+
it('should throw FileNotFoundError if changelog file does not exist', async () => {
71+
const changelogPath = 'CHANGELOG.md';
72+
const error = new Error('File not found') as any;
73+
error.code = 'ENOENT';
74+
vi.spyOn(fileHandler, 'readFile').mockRejectedValue(error);
75+
76+
await expect(changelogService.updateChangelog('## v1.1.0\n\n- New feature')).rejects.toThrow(
77+
new FileNotFoundError(`Changelog file not found at ${changelogPath}`),
78+
);
79+
});
6880
});

0 commit comments

Comments
 (0)