From f04d1342b5ed668f1cc1d80ab9130e1e4b9fcb6a Mon Sep 17 00:00:00 2001 From: gparant Date: Sat, 4 Apr 2020 17:56:43 +0200 Subject: [PATCH] Refactor error message --- back/src/Controller/IoSocketController.ts | 12 ++++++------ back/src/Model/Websocket/MessageUserPosition.ts | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/back/src/Controller/IoSocketController.ts b/back/src/Controller/IoSocketController.ts index 109fda6..5dbacda 100644 --- a/back/src/Controller/IoSocketController.ts +++ b/back/src/Controller/IoSocketController.ts @@ -41,8 +41,8 @@ export class IoSocketController{ socket.on('join-room', (message : string) => { let messageUserPosition = this.hydrateMessageReceive(message); - if(!messageUserPosition){ - return socket.emit("message-error", JSON.stringify({message: "Error format message"})) + if(messageUserPosition instanceof Error){ + return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message})) } //join user in room socket.join(messageUserPosition.roomId); @@ -53,8 +53,8 @@ export class IoSocketController{ socket.on('user-position', (message : string) => { let messageUserPosition = this.hydrateMessageReceive(message); - if(!messageUserPosition){ - return socket.emit("message-error", JSON.stringify({message: "Error format message"})); + if(messageUserPosition instanceof Error){ + return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message})); } // sending to all clients in room except sender this.saveUserPosition((socket as ExSocketInterface), messageUserPosition); @@ -69,12 +69,12 @@ export class IoSocketController{ } //Hydrate and manage error - hydrateMessageReceive(message : string) : MessageUserPosition | null{ + hydrateMessageReceive(message : string) : MessageUserPosition | Error{ try { return new MessageUserPosition(message); }catch (err) { //TODO log error - return null; + return new Error(err); } } } \ No newline at end of file diff --git a/back/src/Model/Websocket/MessageUserPosition.ts b/back/src/Model/Websocket/MessageUserPosition.ts index 60e76ab..731d60f 100644 --- a/back/src/Model/Websocket/MessageUserPosition.ts +++ b/back/src/Model/Websocket/MessageUserPosition.ts @@ -7,7 +7,7 @@ export class Point implements PointInterface{ constructor(x : number, y : number) { if(!x || !y){ - throw Error("x and y cannot be null"); + throw Error("position x and y cannot be null"); } this.x = x; this.y = y; -- 2.25.1