Skip to content

Commit 350e354

Browse files
committed
feat: Introduce custom error types for granular error handling
1 parent 49c0d75 commit 350e354

5 files changed

Lines changed: 97 additions & 135 deletions

File tree

src/errors.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export class PlatformDetectionError extends Error {
2+
constructor(message: string) {
3+
super(message);
4+
this.name = 'PlatformDetectionError';
5+
}
6+
}
7+
8+
export class VersionBumpError extends Error {
9+
constructor(message: string) {
10+
super(message);
11+
this.name = 'VersionBumpError';
12+
}
13+
}

src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
PythonUpdater,
1111
RustUpdater,
1212
} from './updaters';
13+
import { PlatformDetectionError, VersionBumpError } from './errors';
1314

1415
async function run() {
1516
try {
@@ -36,7 +37,11 @@ async function run() {
3637
await commitChanges(`chore: bump version to ${version}`);
3738
await createAndPushTag(version);
3839
} catch (error: unknown) {
39-
if (error instanceof Error) {
40+
if (error instanceof PlatformDetectionError) {
41+
core.setFailed(`Platform detection failed: ${error.message}`);
42+
} else if (error instanceof VersionBumpError) {
43+
core.setFailed(`Version bump failed: ${error.message}`);
44+
} else if (error instanceof Error) {
4045
core.setFailed(error.message);
4146
} else {
4247
core.setFailed(String(error));

src/services/updaterService.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import semver from 'semver';
22
import { Updater } from '../interface';
3+
import { PlatformDetectionError, VersionBumpError } from '../errors';
34

45
export class UpdaterService {
56
private updaters: Updater[];
@@ -10,13 +11,16 @@ export class UpdaterService {
1011

1112
detectPlatform(): string {
1213
const updater = this.updaters.find((u) => u.canHandle());
13-
return updater ? updater.platform : 'unknown';
14+
if (!updater) {
15+
throw new PlatformDetectionError('Could not detect platform.');
16+
}
17+
return updater.platform;
1418
}
1519

1620
updateVersion(platform: string, releaseType: semver.ReleaseType): string {
1721
const updater = this.updaters.find((u) => u.platform === platform);
1822
if (!updater) {
19-
throw new Error(`No updater found for platform: ${platform}`);
23+
throw new VersionBumpError(`No updater found for platform: ${platform}`);
2024
}
2125
return updater.bumpVersion(releaseType);
2226
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { UpdaterService } from '../../src/services';
3+
import { Updater } from '../../src/interface';
4+
import { PlatformDetectionError, VersionBumpError } from '../../src/errors';
5+
6+
const mockNodeUpdater: Updater = {
7+
platform: 'node',
8+
canHandle: vi.fn(),
9+
getCurrentVersion: vi.fn(),
10+
bumpVersion: vi.fn(),
11+
};
12+
13+
const mockPythonUpdater: Updater = {
14+
platform: 'python',
15+
canHandle: vi.fn(),
16+
getCurrentVersion: vi.fn(),
17+
bumpVersion: vi.fn(),
18+
};
19+
20+
const mockUpdaters: Updater[] = [mockNodeUpdater, mockPythonUpdater];
21+
22+
describe('UpdaterService', () => {
23+
let updaterService: UpdaterService;
24+
25+
beforeEach(() => {
26+
updaterService = new UpdaterService(mockUpdaters);
27+
vi.clearAllMocks();
28+
mockNodeUpdater.canHandle.mockReset();
29+
mockNodeUpdater.bumpVersion.mockReset();
30+
mockPythonUpdater.canHandle.mockReset();
31+
mockPythonUpdater.bumpVersion.mockReset();
32+
});
33+
34+
describe('detectPlatform', () => {
35+
it('should return the platform of the first updater that can handle', () => {
36+
mockNodeUpdater.canHandle.mockReturnValue(false);
37+
mockPythonUpdater.canHandle.mockReturnValue(true);
38+
39+
expect(updaterService.detectPlatform()).toBe('python');
40+
expect(mockNodeUpdater.canHandle).toHaveBeenCalled();
41+
expect(mockPythonUpdater.canHandle).toHaveBeenCalled();
42+
});
43+
44+
it('should throw PlatformDetectionError if no updater can handle', () => {
45+
mockNodeUpdater.canHandle.mockReturnValue(false);
46+
mockPythonUpdater.canHandle.mockReturnValue(false);
47+
48+
expect(() => updaterService.detectPlatform()).toThrow(PlatformDetectionError);
49+
expect(() => updaterService.detectPlatform()).toThrow('Could not detect platform.');
50+
});
51+
});
52+
53+
describe('updateVersion', () => {
54+
it('should call bumpVersion on the correct updater and return the new version', () => {
55+
const newVersion = '1.2.4';
56+
mockNodeUpdater.bumpVersion.mockReturnValue(newVersion);
57+
58+
const result = updaterService.updateVersion('node', 'patch');
59+
60+
expect(result).toBe(newVersion);
61+
expect(mockNodeUpdater.bumpVersion).toHaveBeenCalledWith('patch');
62+
expect(mockPythonUpdater.bumpVersion).not.toHaveBeenCalled();
63+
});
64+
65+
it('should throw VersionBumpError if no updater is found for the platform', () => {
66+
expect(() => updaterService.updateVersion('nonexistent', 'patch')).toThrow(VersionBumpError);
67+
expect(() => updaterService.updateVersion('nonexistent', 'patch')).toThrow(
68+
'No updater found for platform: nonexistent',
69+
);
70+
});
71+
});
72+
});

tests/updaterUtil.test.ts

Lines changed: 0 additions & 132 deletions
This file was deleted.

0 commit comments

Comments
 (0)