Skip to content

Commit

Permalink
feat: Implement UnsupportedAsyncHandler.
Browse files Browse the repository at this point in the history
  • Loading branch information
RubenVerborgh committed Jan 20, 2021
1 parent b9a280e commit dd9d873
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,6 @@ export * from './util/RecordObject';
export * from './util/SequenceHandler';
export * from './util/StreamUtil';
export * from './util/TermUtil';
export * from './util/UnsupportedAsyncHandler';
export * from './util/Vocabularies';
export * from './util/WaterfallHandler';
22 changes: 22 additions & 0 deletions src/util/UnsupportedAsyncHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AsyncHandler } from './AsyncHandler';
import { NotImplementedHttpError } from './errors/NotImplementedHttpError';

/**
* Handler that does not support any input and will always throw an error.
*/
export class UnsupportedAsyncHandler extends AsyncHandler<any, never> {
private readonly errorMessage?: string;

public constructor(errorMessage?: string) {
super();
this.errorMessage = errorMessage;
}

public async canHandle(): Promise<never> {
throw new NotImplementedHttpError(this.errorMessage);
}

public async handle(): Promise<never> {
return this.canHandle();
}
}
18 changes: 18 additions & 0 deletions test/unit/util/UnsupportedAsyncHandler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NotImplementedHttpError } from '../../../src/util/errors/NotImplementedHttpError';
import { UnsupportedAsyncHandler } from '../../../src/util/UnsupportedAsyncHandler';

describe('An UnsupportedAsyncHandler', (): void => {
it('throws a default error when no message is set.', async(): Promise<void> => {
const handler = new UnsupportedAsyncHandler();
await expect(handler.canHandle()).rejects.toThrow(NotImplementedHttpError);
await expect(handler.handle()).rejects.toThrow(NotImplementedHttpError);
await expect(handler.handleSafe(null)).rejects.toThrow(NotImplementedHttpError);
});

it('throws the specified error when a message is set.', async(): Promise<void> => {
const handler = new UnsupportedAsyncHandler('custom error');
await expect(handler.canHandle()).rejects.toThrow('custom error');
await expect(handler.handle()).rejects.toThrow('custom error');
await expect(handler.handleSafe(null)).rejects.toThrow('custom error');
});
});

0 comments on commit dd9d873

Please sign in to comment.