Skip to content

Commit

Permalink
Merge pull request #36 from PBTP/fix/socket-exception-filter
Browse files Browse the repository at this point in the history
fix: HttpToSocketExceptionFilter logger 추가
  • Loading branch information
emibgo2 authored Sep 2, 2024
2 parents a10c63e + e5c2ef4 commit 58230c6
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/chat/application/http-to-socket-exception.filter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { ArgumentsHost, Catch, HttpException } from '@nestjs/common';
import { ArgumentsHost, Catch, HttpException, Logger } from '@nestjs/common';
import { BaseWsExceptionFilter } from '@nestjs/websockets';

@Catch(HttpException)
export class HttpToSocketExceptionFilter extends BaseWsExceptionFilter<HttpException> {
private readonly logger = new Logger(HttpToSocketExceptionFilter.name);

// BaseWsExceptionFilter를 상속하면 Ws관련 Exception을 만들 수 있다.

catch(exception: HttpException, host: ArgumentsHost): void {
const status = exception.getStatus();
if (500 <= status) {
this.logger.error('Server error', exception.message);
}

if (400 <= status && status < 500) {
this.logger.warn('BadRequest', exception.message);
}

const socket = host.switchToWs().getClient(); // 소켓 가져오기
socket.emit('exception', exception.getResponse());
}
Expand Down

0 comments on commit 58230c6

Please sign in to comment.