feat: Create ErrorHandler to convert errors to Representations

This commit is contained in:
Joachim Van Herwegen
2021-06-03 16:26:55 +02:00
parent 3ef815ee6d
commit e1f95877da
18 changed files with 486 additions and 28 deletions

View File

@@ -0,0 +1,34 @@
import { assertNativeError, getStatusCode, isNativeError } from '../../../../src/util/errors/ErrorUtil';
import { NotFoundHttpError } from '../../../../src/util/errors/NotFoundHttpError';
describe('ErrorUtil', (): void => {
describe('#isNativeError', (): void => {
it('returns true on native errors.', async(): Promise<void> => {
expect(isNativeError(new Error('error'))).toBe(true);
});
it('returns false on other values.', async(): Promise<void> => {
expect(isNativeError('apple')).toBe(false);
});
});
describe('#assertNativeError', (): void => {
it('returns undefined on native errors.', async(): Promise<void> => {
expect(assertNativeError(new Error('error'))).toBeUndefined();
});
it('throws on other values.', async(): Promise<void> => {
expect((): void => assertNativeError('apple')).toThrow('apple');
});
});
describe('#getStatusCode', (): void => {
it('returns the corresponding status code for HttpErrors.', async(): Promise<void> => {
expect(getStatusCode(new NotFoundHttpError())).toBe(404);
});
it('returns 500 for other errors.', async(): Promise<void> => {
expect(getStatusCode(new Error('404'))).toBe(500);
});
});
});