Add and send Missing_User_Name Exception

landing-page-layout
Laura Valera 2025-02-09 18:21:20 +01:00
parent b878beaaff
commit 496c68550b
5 changed files with 31 additions and 4 deletions

View File

@ -16,7 +16,10 @@ import {
JoinLobbyEvent,
} from 'interface';
import { createWsExceptionFilter } from './websocket-exception-filter';
import { PlayerNotFoundException } from './exceptions';
import {
MissingPlayerNameException,
PlayerNotFoundException,
} from './exceptions';
@WebSocketGateway({ cors: true })
@Injectable()
@ -31,7 +34,12 @@ export class AppService implements OnGatewayConnection {
this.playerService.createPlayer(client.id);
}
@UseFilters(createWsExceptionFilter([PlayerNotFoundException]))
@UseFilters(
createWsExceptionFilter([
PlayerNotFoundException,
MissingPlayerNameException,
]),
)
@SubscribeMessage(ClientEvent.CREATE_LOBBY)
handleCreateLobby(
@ConnectedSocket() client: Socket,
@ -47,6 +55,12 @@ export class AppService implements OnGatewayConnection {
});
}
@UseFilters(
createWsExceptionFilter([
PlayerNotFoundException,
MissingPlayerNameException,
]),
)
@SubscribeMessage(ClientEvent.JOIN_LOBBY)
handleJoinLobby(
@ConnectedSocket() client: Socket,

View File

@ -10,6 +10,12 @@ export abstract class WebSocketException extends WsException {
}
}
export class MissingPlayerNameException extends WebSocketException {
constructor() {
super('Missing player name', ErrorCode.MISSING_USER_NAME);
}
}
export class PlayerNotFoundException extends WebSocketException {
constructor(playerId: string) {
super(

View File

@ -1,6 +1,9 @@
import { Injectable, Logger } from '@nestjs/common';
import { Player } from './player';
import { PlayerNotFoundException } from 'src/exceptions';
import {
MissingPlayerNameException,
PlayerNotFoundException,
} from 'src/exceptions';
@Injectable()
export class PlayerService {
@ -13,6 +16,9 @@ export class PlayerService {
}
addUserName(socketId: string, userName: string) {
if (!userName) {
throw new MissingPlayerNameException();
}
this.players.get(socketId).userName = userName;
}

View File

@ -3,7 +3,7 @@ import { Socket as ClientSocket } from "socket.io-client";
import { ErrorCode, ServerError } from "./ServerError";
export type CreateLobbyError = {
error: ErrorCode.PLAYER_NOT_FOUND | ErrorCode.GAME_NOT_FOUND;
error: ErrorCode;
};
export const emitCreateLobbyError = (

View File

@ -6,4 +6,5 @@ export enum ServerError {
export enum ErrorCode {
PLAYER_NOT_FOUND = "player-not-found",
GAME_NOT_FOUND = "game-not-found",
MISSING_USER_NAME = "player-user-name-not-found",
}