Skip to content

Commit 89524c0

Browse files
Implement FileUtilsError class with unit tests (#44)
- Added `FileUtilsError.ts` to define a custom error class for file-related operations, including optional cause handling. - Created `FileUtilsError.test.ts` to validate the functionality of the `FileUtilsError` class, ensuring proper message, name, and cause assignment. These additions enhance error handling for file operations and improve test coverage for error management.
1 parent 3d7fe8e commit 89524c0

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, expect, it } from 'vitest';
2+
3+
import { FileUtilsError } from './FileUtilsError';
4+
5+
describe('FileUtilsError', () => {
6+
it('should set the message and name', () => {
7+
const err = new FileUtilsError('test message');
8+
9+
expect(err.message).toBe('test message');
10+
expect(err.name).toBe('FileUtilsError');
11+
});
12+
13+
it('should set the cause if provided', () => {
14+
const cause = new Error('root cause');
15+
const err = new FileUtilsError('with cause', cause);
16+
17+
expect(err.cause).toBe(cause);
18+
});
19+
20+
it('should not set cause if not provided', () => {
21+
const err = new FileUtilsError('no cause');
22+
23+
expect(err.cause).toBeUndefined();
24+
});
25+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export class FileUtilsError extends Error {
2+
public cause?: unknown;
3+
4+
constructor(message: string, cause?: unknown) {
5+
super(message);
6+
this.name = 'FileUtilsError';
7+
this.cause = cause;
8+
}
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './FileUtilsError';

0 commit comments

Comments
 (0)