From 821c8211cc6bbc0af7cf6ba9b9506288fc2c00ce Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 31 Mar 2020 16:32:25 +0200 Subject: [PATCH] Improve errors handling --- src/controllers/UserWebSocketController.ts | 21 ++++++++++++--------- src/entities/WebSocketRequestHandler.ts | 4 ++++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/controllers/UserWebSocketController.ts b/src/controllers/UserWebSocketController.ts index e49b515..a2cf631 100644 --- a/src/controllers/UserWebSocketController.ts +++ b/src/controllers/UserWebSocketController.ts @@ -172,15 +172,18 @@ export class UserWebSocketController { await route.handler(handler); } catch(e) { - - // Try again to send again a response - try { - handler.sendResponse("error", { - code: 500, - message: "Server error" - }); - } catch(e) { - console.error(e); + console.error(e); + + // Try to send a server error response + if(!handler.isResponseSent) { + try { + handler.sendResponse("error", { + code: 500, + message: "Server error" + }); + } catch(e) { + console.error(e); + } } } diff --git a/src/entities/WebSocketRequestHandler.ts b/src/entities/WebSocketRequestHandler.ts index 9bff22b..296b05f 100644 --- a/src/entities/WebSocketRequestHandler.ts +++ b/src/entities/WebSocketRequestHandler.ts @@ -15,6 +15,10 @@ export class UserWebSocketRequestsHandler extends BaseRequestsHandler { constructor(private wsClient: ActiveClient, private req: WsMessage) { super(); } + + public get isResponseSent() : boolean { + return this.sentResponse; + } protected get userID(): number { return this.wsClient.userID;