mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 01:23:52 +00:00
add CustomError class
This will be used for throwing errors that can be caught and handled without having to parse the message.
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2022 The Pybricks Authors
|
||||
|
||||
import { CustomError } from './customError';
|
||||
|
||||
// all subclasses should be declared like this:
|
||||
|
||||
type TestErrorName = 'Test';
|
||||
|
||||
class TestError extends CustomError<TestErrorName> {}
|
||||
|
||||
describe('CustomError', () => {
|
||||
it('should work with instanceof', () => {
|
||||
expect(new TestError('Test', 'test')).toBeInstanceOf(TestError);
|
||||
});
|
||||
|
||||
it('should override name', () => {
|
||||
expect(new TestError('Test', 'test')).toHaveProperty('name', 'Test');
|
||||
});
|
||||
|
||||
it('should pass cause', () => {
|
||||
const testCause = new Error('test cause');
|
||||
expect(() => {
|
||||
throw new TestError('Test', 'test', testCause);
|
||||
}).toThrow({ name: 'Test', message: 'test', cause: testCause });
|
||||
});
|
||||
|
||||
it('should serialize to something useful', () => {
|
||||
expect(JSON.stringify(new TestError('Test', 'test'))).toBe(
|
||||
'"[TestError: Test]"',
|
||||
);
|
||||
|
||||
expect(`${new TestError('Test', 'test')}`).toBe('[TestError: Test]');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2022 The Pybricks Authors
|
||||
|
||||
export class CustomError<T extends string> extends Error {
|
||||
public constructor(name: T, message: string, cause?: Error) {
|
||||
super(message, { cause });
|
||||
this.name = name;
|
||||
Object.setPrototypeOf(this, new.target.prototype);
|
||||
}
|
||||
|
||||
public toString(): string {
|
||||
return `[${this.constructor.name}: ${this.name}]`;
|
||||
}
|
||||
|
||||
public toJSON(): string {
|
||||
return this.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user