From 3b707c8601da4c756d0970591c4664fbcddc2b14 Mon Sep 17 00:00:00 2001 From: AlecM33 Date: Sat, 21 Jan 2023 15:48:35 -0500 Subject: [PATCH] add some error handling --- server/api/AdminAPI.js | 13 +++++++++++-- server/modules/singletons/EventManager.js | 10 ++++++++-- server/modules/singletons/GameManager.js | 18 +++++++++++++----- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/server/api/AdminAPI.js b/server/api/AdminAPI.js index aa025f6..2f20445 100644 --- a/server/api/AdminAPI.js +++ b/server/api/AdminAPI.js @@ -3,7 +3,6 @@ const router = express.Router(); const debugMode = Array.from(process.argv.map((arg) => arg.trim().toLowerCase())).includes('debug'); const logger = require('../modules/Logger')(debugMode); const eventManager = (require('../modules/singletons/EventManager.js')).instance; -const timerManager = (require('../modules/singletons/TimerManager.js')).instance; const globals = require('../config/globals.js'); const cors = require('cors'); @@ -24,7 +23,17 @@ router.get('/games/state', async (req, res) => { const gamesArray = []; const keys = await eventManager.client.keys('*'); const values = await eventManager.client.mGet(keys); - values.forEach((v) => gamesArray.push(JSON.parse(v))); + values.forEach((v) => { + let parsedGame; + try { + parsedGame = JSON.parse(v); + } catch (e) { + logger.error(e); + } + if (parsedGame) { + gamesArray.push(parsedGame); + } + }); res.status(200).send(gamesArray); }); diff --git a/server/modules/singletons/EventManager.js b/server/modules/singletons/EventManager.js index edd8da1..9cba5a6 100644 --- a/server/modules/singletons/EventManager.js +++ b/server/modules/singletons/EventManager.js @@ -26,12 +26,18 @@ class EventManager { createRedisPublisher = async () => { this.publisher = redis.createClient(); + this.publisher.on('error', (e) => { + this.logger.error('REDIS PUBLISHER CLIENT ERROR:', e); + }); await this.publisher.connect(); this.logger.info('EVENT MANAGER - CREATED PUBLISHER'); } createGameSyncSubscriber = async (gameManager, eventManager) => { this.subscriber = this.client.duplicate(); + this.subscriber.on('error', (e) => { + this.logger.error('REDIS SUBSCRIBER CLIENT ERROR:', e); + }); await this.subscriber.connect(); await this.subscriber.subscribe(globals.REDIS_CHANNELS.ACTIVE_GAME_STREAM, async (message) => { this.logger.debug('MESSAGE: ' + message); @@ -46,8 +52,8 @@ class EventManager { message.slice( message.indexOf(messageComponents[messageComponents.length - 1]) + (globals.INSTANCE_ID_LENGTH + 1) ) - ) - } catch(e) { + ); + } catch (e) { this.logger.error('MALFORMED MESSAGE RESULTED IN ERROR: ' + e + '; DISREGARDING'); return; } diff --git a/server/modules/singletons/GameManager.js b/server/modules/singletons/GameManager.js index a9027a0..6ede4d5 100644 --- a/server/modules/singletons/GameManager.js +++ b/server/modules/singletons/GameManager.js @@ -28,7 +28,15 @@ class GameManager { } delete this.timerManager.timerThreads[accessCode]; } - return r === null ? r : JSON.parse(r); + let activeGame; + if (r !== null) { + try { + activeGame = JSON.parse(r); + } catch (e) { + this.logger.error('ERROR PARSING ACTIVE GAME: ' + e); + } + } + return r === null ? r : activeGame; } setGameSocketNamespace = (namespace) => { @@ -62,8 +70,8 @@ class GameManager { moderator.assigned = true; if (req.timerParams !== null) { req.timerParams.paused = true; - req.timerParams.timeRemaining = convertFromHoursToMilliseconds(req.timerParams.hours) - + convertFromMinutesToMilliseconds(req.timerParams.minutes); + req.timerParams.timeRemaining = convertFromHoursToMilliseconds(req.timerParams.hours) + + convertFromMinutesToMilliseconds(req.timerParams.minutes); } const newGame = new Game( newAccessCode, @@ -251,8 +259,8 @@ class GameManager { game.status = globals.STATUS.IN_PROGRESS; if (game.hasTimer) { game.timerParams.paused = true; - game.timerParams.timeRemaining = convertFromHoursToMilliseconds(game.timerParams.hours) - + convertFromMinutesToMilliseconds(game.timerParams.minutes); + game.timerParams.timeRemaining = convertFromHoursToMilliseconds(game.timerParams.hours) + + convertFromMinutesToMilliseconds(game.timerParams.minutes); await this.timerManager.runTimer(game, namespace, this.eventManager, this); }