From 86487a834601bda99f5e1be10311d991227a0187 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 2 Jul 2018 09:43:36 +0200 Subject: [PATCH] Error Handling Improved at Server Modules --- Server/BotsScripts.js | 14 ++++++- Server/CloudScripts.js | 39 ++++++++++++++----- Server/ConfigReader.js | 82 ++++++++++++++++++++++++++++++++++------ Server/SessionManager.js | 15 ++++++-- Server/Storage.js | 8 ++-- server.js | 73 +++++++++++++++++++++++++++++++---- 6 files changed, 194 insertions(+), 37 deletions(-) diff --git a/Server/BotsScripts.js b/Server/BotsScripts.js index 9d662f2fc..1f58ade6d 100644 --- a/Server/BotsScripts.js +++ b/Server/BotsScripts.js @@ -26,12 +26,21 @@ storage.readData(pDevTeam + "/" + pSource, pRepo, pPath, false, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { try { if (CONSOLE_LOG === true) { console.log("[INFO] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Entering function."); } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + let data; if (pData !== undefined) { @@ -43,11 +52,12 @@ } - callBackFunction(data); + callBackFunction(global.DEFAULT_OK_RESPONSE, data); } catch (err) { console.log("[ERROR] BotScripts -> retrieveScripts -> Cloud -> onDataArrived -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } diff --git a/Server/CloudScripts.js b/Server/CloudScripts.js index abd880b81..b2d5b61f8 100644 --- a/Server/CloudScripts.js +++ b/Server/CloudScripts.js @@ -27,10 +27,10 @@ storage.initialize(storageData, serverConfig); - callBackFunction(); + callBackFunction(global.DEFAULT_OK_RESPONSE); } - function loadCloudScripts(callBackfunction) { + function loadCloudScripts(callBackFunction) { try { @@ -44,17 +44,26 @@ storage.readData('AdvancedAlgos', 'AACloud', 'web.config.json', true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { try { if (CONSOLE_LOG === true) { console.log("[INFO] CloudScripts -> loadCloudScripts -> Cloud -> onDataArrived -> Entering function."); } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] CloudScripts -> loadCloudScripts -> Cloud -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] CloudScripts -> loadCloudScripts -> Cloud -> onDataArrived -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + let data = pData.toString(); data = data.trim(); // remove first byte with some encoding. dataObject = JSON.parse(data); - retrieveScripts(dataObject, callBackfunction); + retrieveScripts(dataObject, callBackFunction); } catch (err) { console.log("[ERROR] CloudScripts -> loadCloudScripts -> Cloud -> onDataArrived -> Error = " + err); @@ -63,10 +72,11 @@ } catch (err) { console.log("[ERROR] CloudScripts -> loadCloudScripts -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - function retrieveScripts(pCloudWebConfig, callBackfunction) { + function retrieveScripts(pCloudWebConfig, callBackFunction) { try { @@ -86,12 +96,21 @@ storage.readData('AdvancedAlgos', 'AACloud', webModule, true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { try { if (CONSOLE_LOG === true) { console.log("[INFO] CloudScripts -> retrieveScripts -> Cloud -> onDataArrived -> Entering function."); } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] CloudScripts -> retrieveScripts -> Cloud -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] CloudScripts -> retrieveScripts -> Cloud -> onDataArrived -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + let data = pData.toString(); data = data.trim(); // remove first byte with some encoding. @@ -101,12 +120,13 @@ modulesRetrieved++; if (modulesRetrieved === pCloudWebConfig.webModules.length) { - createJS(callBackfunction); + createJS(callBackFunction); } } catch (err) { console.log("[ERROR] CloudScripts -> retrieveScripts -> Cloud -> onDataArrived -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } @@ -125,7 +145,7 @@ } } - function createJS(callBackfunction) { + function createJS(callBackFunction) { let lines = ""; let jsLine = '' + '\n' + ' "AACloud/@module@",' @@ -139,12 +159,13 @@ lines = lines + newLink; } - callBackfunction(lines); + callBackFunction(global.DEFAULT_OK_RESPONSE, lines); } } catch (err) { console.log("[ERROR] CloudScripts -> retrieveScripts -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } \ No newline at end of file diff --git a/Server/ConfigReader.js b/Server/ConfigReader.js index 5bf31cdcb..404fff645 100644 --- a/Server/ConfigReader.js +++ b/Server/ConfigReader.js @@ -59,16 +59,18 @@ storage.initialize(storageData, serverConfig); - callBackFunction(serverConfig); + callBackFunction(global.DEFAULT_OK_RESPONSE, serverConfig); } catch (err) { console.log("[ERROR] ConfigReader -> readAAWebConfig -> onFileRead -> File = " + fileName + " Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } catch (err) { console.log("[ERROR] ConfigReader -> readAAWebConfig -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } @@ -93,12 +95,21 @@ storage.readData('AdvancedAlgos', 'AAPlatform', 'ecosystem.json', true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { try { if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readEcosystemConfig -> Cloud -> onDataArrived -> Entering function."); } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] ConfigReader -> readEcosystemConfig -> Cloud -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] ConfigReader -> readEcosystemConfig -> Cloud -> onDataArrived -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + ecosystem = pData.toString(); ecosystem = ecosystem.trim(); // remove first byte with some encoding. @@ -106,12 +117,14 @@ readHostsConfigs(); } catch (err) { - console.log("[ERROR] ConfigReader -> readEcosystemConfig -> Cloud -> onDataArrived -> Error = " + err); + console.log("[ERROR] ConfigReader -> loadConfigs -> readEcosystemConfig -> onDataArrived -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } catch (err) { - console.log("[ERROR] ConfigReader -> readEcosystemConfig -> Error = " + err); + console.log("[ERROR] ConfigReader -> loadConfigs -> readEcosystemConfig -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -147,7 +160,7 @@ storage.readData(host.codeName + "/" + "competitions", competition.repo, competition.configFile, true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { try { @@ -156,6 +169,15 @@ if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> competition.repo = " + competition.repo); } if (LOG_FILE_CONTENT === true) { console.log("[INFO] ConfigReader -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> pData = " + pData); } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] ConfigReader -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] ConfigReader -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + responsesReceived++; pData = pData.toString(); @@ -179,12 +201,14 @@ } catch (err) { console.log("[ERROR] ConfigReader -> readHostsConfigs -> getCompetitions -> Cloud -> onDataArrived -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } } catch (err) { console.log("[ERROR] ConfigReader -> readHostsConfigs -> getCompetitions -> getCompetitions -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -204,7 +228,7 @@ storage.readData(host.codeName + "/" + "plotters", plotter.repo, plotter.configFile, true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { try { @@ -213,6 +237,15 @@ if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> plotter.repo = " + plotter.repo); } if (LOG_FILE_CONTENT === true) { console.log("[INFO] ConfigReader -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> pData = " + pData); } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] ConfigReader -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] ConfigReader -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + responsesReceived++; pData = pData.toString(); @@ -236,18 +269,21 @@ } catch (err) { console.log("[ERROR] ConfigReader -> readHostsConfigs -> getPlotters -> Cloud -> onDataArrived -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } } catch (err) { console.log("[ERROR] ConfigReader -> readHostsConfigs -> getPlotters -> getCompetitions -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } } catch (err) { console.log("[ERROR] ConfigReader -> readHostsConfigs -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -293,7 +329,7 @@ storage.readData(devTeam.codeName + "/" + "bots", bot.repo, bot.configFile, true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { try { @@ -302,6 +338,15 @@ if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> bot.repo = " + bot.repo); } if (LOG_FILE_CONTENT === true) { console.log("[INFO] ConfigReader -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> pData = " + pData); } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + responsesReceived++; if (pData !== "{}") { // If for any reason we could not tet this config, then we just exclude this bot to improve resiliency. @@ -324,19 +369,21 @@ if (requestsSent === responsesReceived) { - callBackFunction(ecosystem, ecosystemObject); + callBackFunction(global.DEFAULT_OK_RESPONSE, ecosystem, ecosystemObject); } } catch (err) { console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> getBots -> Cloud -> onDataArrived -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } } catch (err) { console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> getBots -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } @@ -356,7 +403,7 @@ storage.readData(devTeam.codeName + "/" + "plotters", plotter.repo, plotter.configFile, true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { try { @@ -365,6 +412,15 @@ if (CONSOLE_LOG === true) { console.log("[INFO] ConfigReader -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> plotter.repo = " + plotter.repo); } if (LOG_FILE_CONTENT === true) { console.log("[INFO] ConfigReader -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> pData = " + pData); } + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } + responsesReceived++; if (pData !== "{}") { // If for any reason we could not tet this config, then we just exclude this bot to improve resiliency. @@ -385,12 +441,13 @@ if (requestsSent === responsesReceived) { - callBackFunction(ecosystem, ecosystemObject); + callBackFunction(global.DEFAULT_OK_RESPONSE, ecosystem, ecosystemObject); } } catch (err) { console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> getPlotters -> Cloud -> onDataArrived -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } @@ -398,6 +455,7 @@ } catch (err) { console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> getPlotters -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } @@ -440,15 +498,15 @@ } catch (err) { console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> addStoragePermissions -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } catch (err) { console.log("[ERROR] ConfigReader -> readDevTeamsConfigs -> Error = " + err); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } - - } } \ No newline at end of file diff --git a/Server/SessionManager.js b/Server/SessionManager.js index b41a17425..412f21eed 100644 --- a/Server/SessionManager.js +++ b/Server/SessionManager.js @@ -23,7 +23,16 @@ storage.initialize(undefined, pServerConfig); storage.readData("AdvancedAlgos", "AAPlatform", "Open.Sessions.json", false, onData); - function onData(pText) { + function onData(err, pText) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] SessionManager -> initialize -> readSessions -> onData -> Could not read a file. "); + console.log("[ERROR] SessionManager -> initialize -> readSessions -> onData -> err.message = " + err.message); + + callBackFunction(global.DEFAULT_FAIL_RESPONSE); + return; + } let sessions = JSON.parse(pText); @@ -35,7 +44,7 @@ } - callBackFunction(); + callBackFunction(global.DEFAULT_OK_RESPONSE); } } @@ -43,7 +52,7 @@ console.log("[ERROR] readSessions -> err = " + err.message); console.log("[HINT] You need to have a file at this path -> " + filePath); - callBackFunction(); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } diff --git a/Server/Storage.js b/Server/Storage.js index 9df403947..36f711cdb 100644 --- a/Server/Storage.js +++ b/Server/Storage.js @@ -90,23 +90,23 @@ if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> err = " + JSON.stringify(err)); } if (CONSOLE_LOG === true) { console.log("[ERROR] Storage -> readData -> onFileReceived -> Returning an empty JSON object string. "); } - callBackFunction(""); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); return; } - callBackFunction(text); + callBackFunction(global.DEFAULT_OK_RESPONSE, text); } catch (err) { console.log("[ERROR] Storage -> readData -> onFileReceived -> err.message = " + err.message); - callBackFunction("{}"); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } } } catch (err) { console.log("[ERROR] Storage -> readData -> err.message = " + err.message); - callBackFunction("{}"); + callBackFunction(global.DEFAULT_FAIL_RESPONSE); } } diff --git a/server.js b/server.js index 78722cb8d..0208ea36f 100644 --- a/server.js +++ b/server.js @@ -67,13 +67,29 @@ function initialize() { configReader.initialize(ecosystem, ecosystemObject, storageData, onInitialized); - function onInitialized(pServerConfig) { + function onInitialized(err, pServerConfig) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] server -> initialize -> onInitialized -> err.message = " + err.message); + console.log("[ERROR] server -> initialize -> onInitialized -> Terminating Execution. "); + + return; + } serverConfig = pServerConfig; configReader.loadConfigs(onConfigsLoaded); - function onConfigsLoaded(pEcosystem, pEcosystemObject) { + function onConfigsLoaded(err, pEcosystem, pEcosystemObject) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] server -> initialize -> onInitialized -> onConfigsLoaded -> err.message = " + err.message); + console.log("[ERROR] server -> initialize -> onInitialized -> onConfigsLoaded -> Terminating Execution. "); + + return; + } ecosystem = pEcosystem; ecosystemObject = pEcosystemObject; @@ -85,11 +101,27 @@ function initialize() { cloudScripts.initialize(ecosystem, ecosystemObject, serverConfig, storageData, onInitialized); - function onInitialized() { + function onInitialized(err) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] server -> initialize -> onInitialized -> onConfigsLoaded -> onInitialized -> err.message = " + err.message); + console.log("[ERROR] server -> initialize -> onInitialized -> onConfigsLoaded -> onInitialized -> Terminating Execution. "); + + return; + } cloudScripts.loadCloudScripts(onLoadCompeted); - function onLoadCompeted(pHTMLCloudScripts) { + function onLoadCompeted(err, pHTMLCloudScripts) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] server -> initialize -> onInitialized -> onConfigsLoaded -> onInitialized -> onLoadCompeted -> err.message = " + err.message); + console.log("[ERROR] server -> initialize -> onInitialized -> onConfigsLoaded -> onInitialized -> onLoadCompeted -> Terminating Execution. "); + + return; + } const SESSION_MANAGER = require('./Server/SessionManager'); sessionManager = SESSION_MANAGER.newSessionManager(); @@ -810,7 +842,16 @@ function onBrowserRequest(request, response) { } } - function onScriptReady(pScript) { + function onScriptReady(err, pScript) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] server -> onBrowserRequest -> Bots -> onScriptReady -> Could not read a file. "); + console.log("[ERROR] server -> onBrowserRequest -> Bots -> onScriptReady -> err.message = " + err.message); + + pScript = "/* Your bot source code could not be retrieved. */"; + + } respondWithContent(pScript, response); @@ -823,7 +864,16 @@ function onBrowserRequest(request, response) { storage.readData(requestParameters[2] + "/" + "plotters", requestParameters[3], requestParameters[4], true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] server -> onBrowserRequest -> Plotters -> onDataArrived -> Could not read a file. "); + console.log("[ERROR] server -> onBrowserRequest -> Plotters -> onDataArrived -> err.message = " + err.message); + + pData = ""; + + } respondWithContent(pData, response); @@ -835,7 +885,16 @@ function onBrowserRequest(request, response) { { storage.readData(requestParameters[2] + "/" + "plotters", requestParameters[3], requestParameters[4], true, onDataArrived); - function onDataArrived(pData) { + function onDataArrived(err, pData) { + + if (err.result !== global.DEFAULT_OK_RESPONSE.result) { + + console.log("[ERROR] server -> onBrowserRequest -> Plotters -> PlotterPanels -> Could not read a file. "); + console.log("[ERROR] server -> onBrowserRequest -> Plotters -> PlotterPanels -> err.message = " + err.message); + + pData = ""; + + } respondWithContent(pData, response);