Simplifying error handling

This commit is contained in:
David Négrier 2021-07-22 10:33:07 +02:00
parent ddabda1c4b
commit 6d4c2cfd39
2 changed files with 39 additions and 61 deletions

View File

@ -45,7 +45,7 @@ const roomManager: IRoomManagerServer = {
let room: GameRoom | null = null;
let user: User | null = null;
call.on("data", (message: PusherToBackMessage) => {
call.on("data", async (message: PusherToBackMessage) => {
try {
if (room === null || user === null) {
if (message.hasJoinroommessage()) {
@ -78,7 +78,11 @@ const roomManager: IRoomManagerServer = {
} else if (message.hasItemeventmessage()) {
socketManager.handleItemEvent(room, user, message.getItemeventmessage() as ItemEventMessage);
} else if (message.hasVariablemessage()) {
socketManager.handleVariableEvent(room, user, message.getVariablemessage() as VariableMessage);
await socketManager.handleVariableEvent(
room,
user,
message.getVariablemessage() as VariableMessage
);
} else if (message.hasWebrtcsignaltoservermessage()) {
socketManager.emitVideo(
room,
@ -119,6 +123,7 @@ const roomManager: IRoomManagerServer = {
}
}
} catch (e) {
console.error(e);
emitError(call, e);
call.end();
}

View File

@ -129,7 +129,6 @@ export class SocketManager {
}
handleUserMovesMessage(room: GameRoom, user: User, userMovesMessage: UserMovesMessage) {
try {
const userMoves = userMovesMessage.toObject();
const position = userMovesMessage.getPosition();
@ -149,10 +148,6 @@ export class SocketManager {
// update position in the world
room.updatePosition(user, ProtobufUtils.toPointInterface(position));
//room.setViewport(client, client.viewport);
} catch (e) {
console.error('An error occurred on "user_position" event');
console.error(e);
}
}
// Useless now, will be useful again if we allow editing details in game
@ -171,18 +166,12 @@ export class SocketManager {
}*/
handleSilentMessage(room: GameRoom, user: User, silentMessage: SilentMessage) {
try {
room.setSilent(user, silentMessage.getSilent());
} catch (e) {
console.error('An error occurred on "handleSilentMessage"');
console.error(e);
}
}
handleItemEvent(room: GameRoom, user: User, itemEventMessage: ItemEventMessage) {
const itemEvent = ProtobufUtils.toItemEvent(itemEventMessage);
try {
const subMessage = new SubMessage();
subMessage.setItemeventmessage(itemEventMessage);
@ -193,21 +182,10 @@ export class SocketManager {
}
room.setItemState(itemEvent.itemId, itemEvent.state);
} catch (e) {
console.error('An error occurred on "item_event"');
console.error(e);
}
}
handleVariableEvent(room: GameRoom, user: User, variableMessage: VariableMessage) {
(async () => {
try {
await room.setVariable(variableMessage.getName(), variableMessage.getValue(), user);
} catch (e) {
console.error('An error occurred on "handleVariableEvent"');
console.error(e);
}
})();
handleVariableEvent(room: GameRoom, user: User, variableMessage: VariableMessage): Promise<void> {
return room.setVariable(variableMessage.getName(), variableMessage.getValue(), user);
}
emitVideo(room: GameRoom, user: User, data: WebRtcSignalToServerMessage): void {
@ -543,17 +521,12 @@ export class SocketManager {
}
emitPlayGlobalMessage(room: GameRoom, playGlobalMessage: PlayGlobalMessage) {
try {
const serverToClientMessage = new ServerToClientMessage();
serverToClientMessage.setPlayglobalmessage(playGlobalMessage);
for (const [id, user] of room.getUsers().entries()) {
user.socket.write(serverToClientMessage);
}
} catch (e) {
console.error('An error occurred on "emitPlayGlobalMessage" event');
console.error(e);
}
}
public getWorlds(): Map<string, PromiseLike<GameRoom>> {