Refactor error message

This commit is contained in:
gparant 2020-04-04 17:56:43 +02:00
parent 53e1600e67
commit f04d1342b5
2 changed files with 7 additions and 7 deletions

View File

@ -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);
}
}
}

View File

@ -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;