refactor: Assign properties directly to error.

This commit is contained in:
Ruben Verborgh
2021-07-12 17:24:08 +01:00
parent 8a62938c18
commit 649f7a9a78
4 changed files with 33 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
import { BadRequestHttpError } from '../../../../src/util/errors/BadRequestHttpError';
import { ConflictHttpError } from '../../../../src/util/errors/ConflictHttpError';
import { ForbiddenHttpError } from '../../../../src/util/errors/ForbiddenHttpError';
import type { HttpErrorOptions } from '../../../../src/util/errors/HttpError';
import { HttpError } from '../../../../src/util/errors/HttpError';
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
import { MethodNotAllowedHttpError } from '../../../../src/util/errors/MethodNotAllowedHttpError';
@@ -11,8 +12,8 @@ import { UnsupportedMediaTypeHttpError } from '../../../../src/util/errors/Unsup
// Only used to make typings easier in the tests
class FixedHttpError extends HttpError {
public constructor(message?: string) {
super(0, '', message);
public constructor(message?: string, options?: HttpErrorOptions) {
super(0, '', message, options);
}
}
@@ -31,7 +32,12 @@ describe('HttpError', (): void => {
];
describe.each(errors)('%s', (name, statusCode, constructor): void => {
const instance = new constructor('my message');
const options = {
cause: new Error('cause'),
errorCode: 'E1234',
details: {},
};
const instance = new constructor('my message', options);
it(`is an instance of ${name}.`, (): void => {
expect(constructor.isInstance(instance)).toBeTruthy();
@@ -48,5 +54,17 @@ describe('HttpError', (): void => {
it('sets the message.', (): void => {
expect(instance.message).toBe('my message');
});
it('sets the cause.', (): void => {
expect(instance.cause).toBe(options.cause);
});
it('sets the error code.', (): void => {
expect(instance.errorCode).toBe(options.errorCode);
});
it('sets the details.', (): void => {
expect(instance.details).toBe(options.details);
});
});
});

View File

@@ -23,7 +23,7 @@ describe('A BaseIdentifierStrategy', (): void => {
expect((): any => strategy.getParentContainer({ path: '/unsupported' }))
.toThrow('The identifier /unsupported is outside the configured identifier space.');
expect((): any => strategy.getParentContainer({ path: '/unsupported' }))
.toThrow(expect.objectContaining({ options: { errorCode: 'E0001', details: { path: '/unsupported' }}}));
.toThrow(expect.objectContaining({ errorCode: 'E0001', details: { path: '/unsupported' }}));
});
it('errors when attempting to get the parent of a root container.', async(): Promise<void> => {