Merge pull request #8802 from wSedlacek/fix/interceptor-check-headers-sent

fix: only send exception responses if header is not already sent
This commit is contained in:
Kamil Mysliwiec
2022-06-20 15:00:38 +02:00
committed by GitHub
6 changed files with 36 additions and 2 deletions

View File

@@ -50,8 +50,10 @@ export interface HttpServer<TRequest = any, TResponse = any> {
listen(port: number | string, hostname: string, callback?: () => void): any;
reply(response: any, body: any, statusCode?: number): any;
status(response: any, statusCode: number): any;
end(response: any, message?: string): any;
render(response: any, view: string, options: any): any;
redirect(response: any, statusCode: number, url: string): any;
isHeadersSent(response: any): boolean;
setHeader(response: any, name: string, value: string): any;
setErrorHandler?(handler: Function, prefix?: string): any;
setNotFoundHandler?(handler: Function, prefix?: string): any;

View File

@@ -105,10 +105,12 @@ export abstract class AbstractHttpAdapter<
abstract getRequestUrl(request);
abstract status(response, statusCode: number);
abstract reply(response, body: any, statusCode?: number);
abstract end(response, message?: string);
abstract render(response, view: string, options: any);
abstract redirect(response, statusCode: number, url: string);
abstract setErrorHandler(handler: Function, prefix?: string);
abstract setNotFoundHandler(handler: Function, prefix?: string);
abstract isHeadersSent(response);
abstract setHeader(response, name: string, value: string);
abstract registerParserMiddleware(prefix?: string, rawBody?: boolean);
abstract enableCors(

View File

@@ -38,7 +38,12 @@ export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
message: res,
};
applicationRef.reply(host.getArgByIndex(1), message, exception.getStatus());
const response = host.getArgByIndex(1);
if (!applicationRef.isHeadersSent(response)) {
applicationRef.reply(response, message, exception.getStatus());
} else {
applicationRef.end(response);
}
}
public handleUnknownError(
@@ -55,7 +60,14 @@ export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
message: MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
};
applicationRef.reply(host.getArgByIndex(1), body, body.statusCode);
const response = host.getArgByIndex(1);
if (!applicationRef.isHeadersSent(response)) {
applicationRef.reply(response, body, body.statusCode);
} else {
applicationRef.end(response);
}
if (this.isExceptionObject(exception)) {
return BaseExceptionFilter.logger.error(
exception.message,

View File

@@ -14,11 +14,13 @@ export class NoopHttpAdapter extends AbstractHttpAdapter {
getRequestMethod(request: any): any {}
getRequestUrl(request: any): any {}
reply(response: any, body: any): any {}
end(response: any, message?: any): any {}
status(response: any, statusCode: number): any {}
render(response: any, view: string, options: any): any {}
redirect(response: any, statusCode: number, url: string) {}
setErrorHandler(handler: Function, prefix = '/'): any {}
setNotFoundHandler(handler: Function, prefix = '/'): any {}
isHeadersSent(response: any): any {}
setHeader(response: any, name: string, value: string): any {}
registerParserMiddleware(): any {}
enableCors(options: any): any {}

View File

@@ -85,6 +85,10 @@ export class ExpressAdapter extends AbstractHttpAdapter {
return response.status(statusCode);
}
public end(response: any, message?: string) {
return response.end(message);
}
public render(response: any, view: string, options: any) {
return response.render(view, options);
}
@@ -101,6 +105,10 @@ export class ExpressAdapter extends AbstractHttpAdapter {
return this.use(handler);
}
public isHeadersSent(response: any): boolean {
return response.headersSent;
}
public setHeader(response: any, name: string, value: string) {
return response.set(name, value);
}

View File

@@ -325,6 +325,10 @@ export class FastifyAdapter<
return (response as TReply).code(statusCode);
}
public end(response: TReply, message?: string) {
response.raw.end(message);
}
public render(
response: TReply & { view: Function },
view: string,
@@ -409,6 +413,10 @@ export class FastifyAdapter<
);
}
public isHeadersSent(response: TReply): boolean {
return response.sent;
}
public setHeader(response: TReply, name: string, value: string) {
return response.header(name, value);
}