From 4ad97ccded1955e168eef6f767d3e72f34d978f6 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Thu, 6 Jun 2024 10:09:42 -0400 Subject: [PATCH] fix(server): closed connections (#10013) --- server/src/middleware/error.interceptor.ts | 14 ++++++-------- server/src/middleware/http-exception.filter.ts | 10 ++++++---- server/src/utils/file.ts | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/server/src/middleware/error.interceptor.ts b/server/src/middleware/error.interceptor.ts index 8d1a25d44c..a0c333e4b2 100644 --- a/server/src/middleware/error.interceptor.ts +++ b/server/src/middleware/error.interceptor.ts @@ -9,7 +9,7 @@ import { } from '@nestjs/common'; import { Observable, catchError, throwError } from 'rxjs'; import { ILoggerRepository } from 'src/interfaces/logger.interface'; -import { isConnectionAborted, routeToErrorMessage } from 'src/utils/misc'; +import { routeToErrorMessage } from 'src/utils/misc'; @Injectable() export class ErrorInterceptor implements NestInterceptor { @@ -21,15 +21,13 @@ export class ErrorInterceptor implements NestInterceptor { return next.handle().pipe( catchError((error) => throwError(() => { - if (error instanceof HttpException === false) { - const errorMessage = routeToErrorMessage(context.getHandler().name); - if (!isConnectionAborted(error)) { - this.logger.error(errorMessage, error, error?.errors, error?.stack); - } - return new InternalServerErrorException(errorMessage); - } else { + if (error instanceof HttpException) { return error; } + + const errorMessage = routeToErrorMessage(context.getHandler().name); + this.logger.error(errorMessage, error, error?.errors, error?.stack); + return new InternalServerErrorException(errorMessage); }), ), ); diff --git a/server/src/middleware/http-exception.filter.ts b/server/src/middleware/http-exception.filter.ts index 0d6de8b276..3306b50ca6 100644 --- a/server/src/middleware/http-exception.filter.ts +++ b/server/src/middleware/http-exception.filter.ts @@ -29,9 +29,11 @@ export class HttpExceptionFilter implements ExceptionFilter { }; } - response.status(status).json({ - ...responseBody, - correlationId: this.cls.getId(), - }); + if (!response.headersSent) { + response.status(status).json({ + ...responseBody, + correlationId: this.cls.getId(), + }); + } } } diff --git a/server/src/utils/file.ts b/server/src/utils/file.ts index 8170fba4e9..c70352af84 100644 --- a/server/src/utils/file.ts +++ b/server/src/utils/file.ts @@ -65,7 +65,7 @@ export const sendFile = async ( await access(file.path, constants.R_OK); - return _sendFile(file.path, options); + return await _sendFile(file.path, options); } catch (error: Error | any) { // ignore client-closed connection if (isConnectionAborted(error)) {