mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
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:
@@ -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;
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 {}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user