Skip to content

Commit

Permalink
๐Ÿ”ง fix : Websocket ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋กœ ๋ณ€๊ฒฝ
Browse files Browse the repository at this point in the history
  • Loading branch information
jinddings committed Dec 3, 2024
1 parent 6608f7e commit b7559f7
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions BE/src/common/filters/websocket-exception.filter.ts
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));
}
}

0 comments on commit b7559f7

Please sign in to comment.