-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement UnsupportedAsyncHandler.
- Loading branch information
1 parent
b9a280e
commit dd9d873
Showing
3 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |