mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
feat: Implement UnsupportedAsyncHandler.
This commit is contained in:
parent
b9a280e64c
commit
dd9d873122
@ -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
src/util/UnsupportedAsyncHandler.ts
Normal file
22
src/util/UnsupportedAsyncHandler.ts
Normal file
@ -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
test/unit/util/UnsupportedAsyncHandler.test.ts
Normal file
18
test/unit/util/UnsupportedAsyncHandler.test.ts
Normal file
@ -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');
|
||||
});
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user