fix: Do not reuse the same error in StaticThrowHandler

This commit is contained in:
Joachim Van Herwegen
2024-04-05 10:25:52 +02:00
parent 2846c711ab
commit f73dfb31c0
3 changed files with 32 additions and 6 deletions

View File

@@ -1,11 +1,11 @@
import type { HttpError } from '../errors/HttpError';
import type { HttpError, HttpErrorClass } from '../errors/HttpError';
import { AsyncHandler } from './AsyncHandler';
/**
* Utility handler that can handle all input and always throws the given error.
* Utility handler that can handle all input and always throws an instance of the given error.
*/
export class StaticThrowHandler extends AsyncHandler<unknown, never> {
private readonly error: HttpError;
protected readonly error: HttpError;
public constructor(error: HttpError) {
super();
@@ -13,6 +13,8 @@ export class StaticThrowHandler extends AsyncHandler<unknown, never> {
}
public async handle(): Promise<never> {
throw this.error;
// We are creating a new instance of the error instead of rethrowing the error,
// as reusing the same error can cause problem as the metadata is then also reused.
throw new (this.error.constructor as HttpErrorClass)();
}
}