diff --git a/app/src/app.service.ts b/app/src/app.service.ts index f1ccff9..0e4272b 100644 --- a/app/src/app.service.ts +++ b/app/src/app.service.ts @@ -10,7 +10,7 @@ import { PlayerService } from './players/player.service'; import { GameService } from 'src/games/game.service'; import { Socket } from 'socket.io'; import { ClientEvent, CreateLobbyEvent, JoinLobbyEvent } from 'interface'; -import { WsExceptionFilter } from './websocket-exception-filter'; +import { createWsExceptionFilter } from './websocket-exception-filter'; import { PlayerNotFoundException } from './exceptions'; @WebSocketGateway({ cors: true }) @@ -26,7 +26,7 @@ export class AppService implements OnGatewayConnection { this.playerService.createPlayer(client.id); } - @UseFilters(new WsExceptionFilter([PlayerNotFoundException])) + @UseFilters(createWsExceptionFilter([PlayerNotFoundException])) @SubscribeMessage(ClientEvent.CREATE_LOBBY) handleCreateLobby( @ConnectedSocket() client: Socket, diff --git a/app/src/exceptions.ts b/app/src/exceptions.ts index 0fb2095..e845efd 100644 --- a/app/src/exceptions.ts +++ b/app/src/exceptions.ts @@ -21,9 +21,6 @@ export class PlayerNotFoundException extends WebSocketException { export class GameNotFoundException extends WebSocketException { constructor(gameId: string) { - super( - 'Unable to find game from ID: ' + gameId, - ErrorCode.GAME_NOT_FOUND, - ); + super('Unable to find game from ID: ' + gameId, ErrorCode.GAME_NOT_FOUND); } } diff --git a/app/src/websocket-exception-filter.ts b/app/src/websocket-exception-filter.ts index a1535d1..9d1b61e 100644 --- a/app/src/websocket-exception-filter.ts +++ b/app/src/websocket-exception-filter.ts @@ -1,44 +1,34 @@ import { ArgumentsHost, Catch, Logger } from '@nestjs/common'; import { BaseWsExceptionFilter } from '@nestjs/websockets'; -import { GameNotFoundException, PlayerNotFoundException, WebSocketException } from './exceptions'; +import { WebSocketException } from './exceptions'; import { Socket } from 'socket.io'; import { ClientEvent } from 'interface'; import { emitCreateLobbyError } from 'interface'; -@Catch(PlayerNotFoundException, GameNotFoundException) -export class WsExceptionFilter extends BaseWsExceptionFilter { - private readonly logger = new Logger(BaseWsExceptionFilter.name); - private readonly caughtExceptions: (typeof WebSocketException)[]; +export const createWsExceptionFilter: ( + exceptionList: (typeof WebSocketException)[], +) => BaseWsExceptionFilter = (handledExceptions) => { + @Catch(...handledExceptions) + class WsExceptionFilter extends BaseWsExceptionFilter { + private readonly logger = new Logger(BaseWsExceptionFilter.name); - constructor(caughtExceptions: (typeof WebSocketException)[]) { - super(); - this.caughtExceptions = caughtExceptions; - } - - catch(exception: WebSocketException, host: ArgumentsHost) { - const socket = host.switchToWs().getClient() as Socket; - const method = host.switchToWs().getPattern(); - const data: object = host.switchToWs().getData(); - this.logger.log( - `Caught exception: ${exception} + catch(exception: WebSocketException, host: ArgumentsHost) { + const socket = host.switchToWs().getClient() as Socket; + const method = host.switchToWs().getPattern(); + const data: object = host.switchToWs().getData(); + this.logger.log( + `Caught exception: ${exception} for request to ${method} from client ${socket.id} - with input ${data}`, - ); + with input ${JSON.stringify(data)}`, + ); - if ( - !this.caughtExceptions.find( - (caughtException) => exception instanceof caughtException, - ) - ) { - this.logger.fatal("Uncaught exception", exception) - super.catch(exception, host); - } - - switch (method) { - case ClientEvent.CREATE_LOBBY: - emitCreateLobbyError(socket, { error: exception.errorCode }); - break; + switch (method) { + case ClientEvent.CREATE_LOBBY: + emitCreateLobbyError(socket, { error: exception.errorCode }); + break; + } } } -} + return new WsExceptionFilter(); +};