From 28b311b7ed37a8bd4c00268b56fd5972a31bc602 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 16 Aug 2018 14:36:11 +0100 Subject: [PATCH] Improve error reporting from context plugin loading --- red/runtime/index.js | 4 +-- red/runtime/locales/en-US/runtime.json | 7 ++++- red/runtime/nodes/context/index.js | 2 ++ red/runtime/nodes/context/localfilesystem.js | 32 ++++++++++++++------ 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/red/runtime/index.js b/red/runtime/index.js index 19e3dc17e..4366466e1 100644 --- a/red/runtime/index.js +++ b/red/runtime/index.js @@ -162,12 +162,10 @@ function start() { if (settings.httpStatic) { log.info(log._("runtime.paths.httpStatic",{path:path.resolve(settings.httpStatic)})); } - redNodes.loadContextsPlugin().then(function () { + return redNodes.loadContextsPlugin().then(function () { redNodes.loadFlows().then(redNodes.startFlows).catch(function(err) {}); started = true; }); - }).catch(function(err) { - console.log(err); }); }); } diff --git a/red/runtime/locales/en-US/runtime.json b/red/runtime/locales/en-US/runtime.json index 1bb6d0cae..a38f44d08 100644 --- a/red/runtime/locales/en-US/runtime.json +++ b/red/runtime/locales/en-US/runtime.json @@ -163,7 +163,12 @@ "error-module-not-defined": "Context store '__storage__' missing 'module' option", "error-invalid-module-name": "Invalid context store name: '__name__'", "error-invalid-default-module": "Default context store unknown: '__storage__'", - "unknown-store": "Unknown context store '__name__' specified. Using default store." + "unknown-store": "Unknown context store '__name__' specified. Using default store.", + "error-loading-module": "Error loading context store: __message__", + "localfilesystem": { + "error-circular": "Context __scope__ contains a circular reference that cannot be persisted", + "error-write": "Error writing context: __message__" + } } } diff --git a/red/runtime/nodes/context/index.js b/red/runtime/nodes/context/index.js index da7c4f670..be79eb00e 100644 --- a/red/runtime/nodes/context/index.js +++ b/red/runtime/nodes/context/index.js @@ -170,6 +170,8 @@ function load() { defaultStore = "memory"; } return resolve(Promise.all(promises)); + }).catch(function(err) { + throw new Error(log._("context.error-loading-module",{message:err.toString()})); }); } diff --git a/red/runtime/nodes/context/localfilesystem.js b/red/runtime/nodes/context/localfilesystem.js index ff9f11d8e..dc25b6290 100644 --- a/red/runtime/nodes/context/localfilesystem.js +++ b/red/runtime/nodes/context/localfilesystem.js @@ -104,8 +104,6 @@ function loadFile(storagePath){ }else{ return Promise.resolve(undefined); } - }).catch(function(err){ - throw Promise.reject(err); }); } @@ -113,9 +111,21 @@ function listFiles(storagePath) { var promises = []; return fs.readdir(storagePath).then(function(files) { files.forEach(function(file) { - promises.push(fs.readdir(path.join(storagePath,file)).then(function(subdirFiles) { - return subdirFiles.map(subfile => path.join(file,subfile)); - })) + if (!/^\./.test(file)) { + var fullPath = path.join(storagePath,file); + var stats = fs.statSync(fullPath); + if (stats.isDirectory()) { + promises.push(fs.readdir(fullPath).then(function(subdirFiles) { + var result = []; + subdirFiles.forEach(subfile => { + if (/\.json$/.test(subfile)) { + result.push(path.join(file,subfile)) + } + }); + return result; + })) + } + } }); return Promise.all(promises); }).then(dirs => dirs.reduce((acc, val) => acc.concat(val), [])); @@ -172,7 +182,7 @@ LocalFileSystem.prototype.open = function(){ if(err.code == 'ENOENT') { return fs.ensureDir(self.storageBaseDir); }else{ - return Promise.reject(err); + throw err; } }).then(function() { self._flushPendingWrites = function() { @@ -185,7 +195,7 @@ LocalFileSystem.prototype.open = function(){ var context = newContext[scope]; var stringifiedContext = stringify(context); if (stringifiedContext.circular && !self.knownCircularRefs[scope]) { - log.warn("Context "+scope+" contains a circular reference that cannot be persisted"); + log.warn(log._("error-circular",{scope:scope})); self.knownCircularRefs[scope] = true; } else { delete self.knownCircularRefs[scope]; @@ -249,7 +259,11 @@ LocalFileSystem.prototype.set = function(scope, key, value, callback) { // there's a pending write which will handle this return; } else { - this._pendingWriteTimeout = setTimeout(function() { self._flushPendingWrites.call(self)}, this.flushInterval); + this._pendingWriteTimeout = setTimeout(function() { + self._flushPendingWrites.call(self).catch(function(err) { + log.error(log._("context.localfilesystem.error-write",{message:err.toString()})) + }); + }, this.flushInterval); } } else if (callback && typeof callback !== 'function') { throw new Error("Callback must be a function"); @@ -272,7 +286,7 @@ LocalFileSystem.prototype.set = function(scope, key, value, callback) { } var stringifiedContext = stringify(obj); if (stringifiedContext.circular && !self.knownCircularRefs[scope]) { - log.warn("Context "+scope+" contains a circular reference that cannot be persisted"); + log.warn(log._("error-circular",{scope:scope})); self.knownCircularRefs[scope] = true; } else { delete self.knownCircularRefs[scope];