Skip to content

Commit

Permalink
Feature(hyunsoo/chats): 채팅서버에서 발생한 에러를 처리하는 필터 추가 modern-agile-team#92
Browse files Browse the repository at this point in the history
  • Loading branch information
KimSoo committed Mar 15, 2023
1 parent 66db631 commit 72d1408
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion main-project/src/chats/chats.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
WebSocketGateway,
WebSocketServer,
} from '@nestjs/websockets';
import { UseGuards, UsePipes } from '@nestjs/common/decorators';
import { UseFilters, UseGuards, UsePipes } from '@nestjs/common/decorators';
import { AsyncApiSub } from 'nestjs-asyncapi';
import { Namespace, Socket } from 'socket.io';
import { WebSocketAuthGuard } from 'src/common/guards/ws-jwt-auth.guard';
Expand All @@ -19,12 +19,14 @@ import { WebSocketTransactionManager } from 'src/common/decorator/ws-transaction
import { WebSocketTransactionInterceptor } from 'src/common/interceptor/ws-transaction-interceptor';
import { EntityManager } from 'typeorm';
import { SendMeetingDto } from './dto/send-meeting.dto';
import { WebSocketExceptionFilter } from 'src/common/exceptions/ws-exception-filter';

@WebSocketGateway(4000, {
namespace: 'chat',
cors: true,
})
@UsePipes(new ValidationPipe())
@UseFilters(new WebSocketExceptionFilter())
export class ChatsGateway {
constructor(private readonly chatGatewayService: ChatsGatewayService) {}

Expand Down
23 changes: 23 additions & 0 deletions main-project/src/common/exceptions/ws-exception-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpStatus,
} from '@nestjs/common';
import { Socket } from 'socket.io';

@Catch()
export class WebSocketExceptionFilter implements ExceptionFilter {
catch(exception, host: ArgumentsHost) {
const ctx = host.switchToWs();
const client = ctx.getClient<Socket>();

client.emit('error', {
error:
exception instanceof Error
? exception.message
: 'Internal server error',
message: exception.response.message,
});
}
}

0 comments on commit 72d1408

Please sign in to comment.