-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
๐ง fix : Websocket ์์ธ์ฒ๋ฆฌ๋ก ๋ณ๊ฒฝ
- Loading branch information
Showing
1 changed file
with
14 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,28 @@ | ||
import { | ||
ArgumentsHost, | ||
Catch, | ||
ExceptionFilter, | ||
HttpStatus, | ||
Logger, | ||
InternalServerErrorException, | ||
} from '@nestjs/common'; | ||
import { ArgumentsHost, Catch, ExceptionFilter, Logger } from '@nestjs/common'; | ||
import { WsException } from '@nestjs/websockets'; | ||
|
||
import { Request, Response } from 'express'; | ||
|
||
@Catch(InternalServerErrorException) | ||
@Catch() | ||
export class WebSocketExceptionFilter implements ExceptionFilter { | ||
private readonly logger = new Logger(WebSocketExceptionFilter.name); | ||
|
||
catch(exception: InternalServerErrorException, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
|
||
const request = ctx.getRequest<Request>(); | ||
const response = ctx.getResponse<Response>(); | ||
|
||
const status = HttpStatus.INTERNAL_SERVER_ERROR; | ||
catch(exception: Error, host: ArgumentsHost) { | ||
const ctx = host.switchToWs(); | ||
const client = ctx.getClient(); | ||
|
||
const { message } = exception; | ||
const message = | ||
exception instanceof WsException | ||
? exception.message | ||
: 'Internal WebSocket Error'; | ||
|
||
const errorResponse = { | ||
statusCode: status, | ||
event: 'error', | ||
message, | ||
timestamp: new Date().toISOString(), | ||
path: request.url, | ||
}; | ||
|
||
this.logger.error( | ||
`[${request.method}] ${request.url} - Status: ${status} - Error: ${exception.message}`, | ||
); | ||
this.logger.error(`WebSocket Error: ${message}`, exception.stack); | ||
|
||
response.status(status).json(errorResponse); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call | ||
client.send(JSON.stringify(errorResponse)); | ||
} | ||
} |