Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: only send exception responses if header is not already sent #8802

Merged
2 changes: 2 additions & 0 deletions packages/common/interfaces/http/http-server.interface.ts
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions packages/core/adapters/http-adapter.ts
Expand Up @@ -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);
abstract enableCors(
Expand Down
16 changes: 14 additions & 2 deletions packages/core/exceptions/base-exception-filter.ts
Expand Up @@ -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(
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/test/utils/noop-adapter.spec.ts
Expand Up @@ -13,11 +13,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 {}
Expand Down
8 changes: 8 additions & 0 deletions packages/platform-express/adapters/express-adapter.ts
Expand Up @@ -60,6 +60,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);
}
Expand All @@ -76,6 +80,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);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/platform-fastify/adapters/fastify-adapter.ts
Expand Up @@ -300,6 +300,10 @@ export class FastifyAdapter<
return (response as TReply).code(statusCode);
}

public end(response: TReply, message?: string) {
response.raw.end(message !== undefined ? String(message) : undefined);
kamilmysliwiec marked this conversation as resolved.
Show resolved Hide resolved
}

public render(
response: TReply & { view: Function },
view: string,
Expand Down Expand Up @@ -384,6 +388,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);
}
Expand Down