Enforce strongly typed API contract in exception filters
parent
5d55631812
commit
e2bf337b25
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<WebSocketException> {
|
||||
private readonly logger = new Logger(BaseWsExceptionFilter.name);
|
||||
private readonly caughtExceptions: (typeof WebSocketException)[];
|
||||
export const createWsExceptionFilter: (
|
||||
exceptionList: (typeof WebSocketException)[],
|
||||
) => BaseWsExceptionFilter<WebSocketException> = (handledExceptions) => {
|
||||
@Catch(...handledExceptions)
|
||||
class WsExceptionFilter extends BaseWsExceptionFilter<WebSocketException> {
|
||||
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();
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue