Skip to content

Commit

Permalink
πŸš‘ !HOTFIX : socket gate way filter μΆ”κ°€
Browse files Browse the repository at this point in the history
  • Loading branch information
jinddings committed Dec 3, 2024
1 parent fcf2cb5 commit 6608f7e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
39 changes: 39 additions & 0 deletions BE/src/common/filters/websocket-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpStatus,
Logger,
InternalServerErrorException,
} from '@nestjs/common';

import { Request, Response } from 'express';

@Catch(InternalServerErrorException)
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;

const { message } = exception;

const errorResponse = {
statusCode: status,
message,
timestamp: new Date().toISOString(),
path: request.url,
};

this.logger.error(
`[${request.method}] ${request.url} - Status: ${status} - Error: ${exception.message}`,
);

response.status(status).json(errorResponse);
}
}
4 changes: 3 additions & 1 deletion BE/src/common/websocket/socket.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { WebSocketGateway, WebSocketServer } from '@nestjs/websockets';
import { Server } from 'socket.io';
import { Logger } from '@nestjs/common';
import { Logger, UseFilters } from '@nestjs/common';
import { WebSocketExceptionFilter } from '../filters/websocket-exception.filter';

@WebSocketGateway({ namespace: 'socket', cors: { origin: '*' } })
@UseFilters(new WebSocketExceptionFilter())
export class SocketGateway {
@WebSocketServer()
private server: Server;
Expand Down

0 comments on commit 6608f7e

Please sign in to comment.