1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Implemented error handlings

This commit is contained in:
Kazuki-Nakanishi 2018-03-28 15:54:16 +09:00 committed by HirokiUchikawa
parent e33ec0cf50
commit 3a476ac493
2 changed files with 209 additions and 98 deletions

View File

@ -21,12 +21,13 @@ var settings;
var contexts = {}; var contexts = {};
var globalContext = null; var globalContext = null;
var externalContexts = {}; var externalContexts = {};
var noContextStorage = false;
function init(_settings) { function init(_settings) {
settings = _settings; settings = _settings;
externalContexts = {}; externalContexts = {};
// init meomory plugin // init memory plugin
externalContexts["_"] = require("./memory"); externalContexts["_"] = require("./memory");
externalContexts["_"].init(); externalContexts["_"].init();
globalContext = createContext("global",settings.functionGlobalContext || {}); globalContext = createContext("global",settings.functionGlobalContext || {});
@ -37,6 +38,7 @@ function load() {
var plugins = settings.contextStorage; var plugins = settings.contextStorage;
var alias = null; var alias = null;
if (plugins) { if (plugins) {
noContextStorage = false;
for(var pluginName in plugins){ for(var pluginName in plugins){
if(pluginName === "_"){ if(pluginName === "_"){
continue; continue;
@ -49,24 +51,30 @@ function load() {
if(plugins[pluginName].hasOwnProperty("module")){ if(plugins[pluginName].hasOwnProperty("module")){
var config = plugins[pluginName].config || {}; var config = plugins[pluginName].config || {};
copySettings(config, settings); copySettings(config, settings);
if(typeof plugins[pluginName].module === "string") {
try{ try{
plugin = require("./"+plugins[pluginName].module); plugin = require("./"+plugins[pluginName].module);
}catch(err){ }catch(err){
throw new Error(plugins[pluginName].module + " could not be loaded"); throw new Error(plugins[pluginName].module + " could not be loaded");
} }
} else {
plugin = plugins[pluginName].module;
}
plugin.init(config); plugin.init(config);
externalContexts[pluginName] = plugin; externalContexts[pluginName] = plugin;
}else{ }else{
throw new Error("module is is not defined in settings.contextStorage." + plugins[pluginName] ); throw new Error("module is not defined in settings.contextStorage." + pluginName );
} }
} }
if(alias){ if(alias){
if(externalContexts.hasOwnProperty(alias)){ if(externalContexts.hasOwnProperty(alias)){
externalContexts["default"] = externalContexts[alias]; externalContexts["default"] = externalContexts[alias];
}else{ }else{
throw new Error("default is invalid" + plugins["default"]) throw new Error("default is invalid. module name=" + plugins["default"])
} }
} }
} else {
noContextStorage = true;
} }
} }
@ -78,35 +86,49 @@ function copySettings(config, settings){
}); });
} }
function parseKey(key){
if(!key){
return null;
}
var keyPath = {storage: "", key: ""};
var index_$ = key.indexOf("$");
var index_dot = key.indexOf(".", 1);
if(index_$===0&&index_dot) {
keyPath.storage = key.substring(1,index_dot)||"default";
keyPath.key = key.substring(index_dot+1);
} else {
keyPath.key = key;
}
return keyPath;
}
function getContextStorage(keyPath) {
if (noContextStorage || !keyPath.storage) {
return externalContexts["_"];
} else if (externalContexts.hasOwnProperty(keyPath.storage)) {
return externalContexts[keyPath.storage];
} else if (externalContexts.hasOwnProperty("default")) {
return externalContexts["default"];
} else {
var contextError = new Error(keyPath.storage + " is not defined in contextStorage on settings.js");
contextError.name = "ContextError";
throw contextError;
}
}
function createContext(id,seed) { function createContext(id,seed) {
var scope = id; var scope = id;
var obj = seed || {}; var obj = seed || {};
obj.get = function(key) { obj.get = function(key) {
var result = parseKey(key); var keyPath = parseKey(key);
if(!result){ var context = getContextStorage(keyPath);
return externalContexts["_"].get(key, scope); return context.get(keyPath.key, scope);
}
if(externalContexts.hasOwnProperty(result[0])){
return externalContexts[result[0]].get(result[1], scope);
}else if(externalContexts.hasOwnProperty("defalut")){
return externalContexts["defalut"].get(result[1], scope);
}else{
throw new Error(result[0] + " is not defined in setting.js");
}
}; };
obj.set = function(key, value) { obj.set = function(key, value) {
var result = parseKey(key); var keyPath = parseKey(key);
if(!result){ var context = getContextStorage(keyPath);
return externalContexts["_"].set(key, value, scope); return context.set(keyPath.key, value, scope);
}
if(externalContexts.hasOwnProperty(result[0])){
externalContexts[result[0]].set(result[1], value, scope);
}else if(externalContexts.hasOwnProperty("defalut")){
externalContexts["defalut"].set(result[1], value, scope);
}else{
throw new Error(result[0] + " is not defined in setting.js");
}
}; };
obj.keys = function() { obj.keys = function() {
//TODO: discuss about keys() behavior //TODO: discuss about keys() behavior
@ -169,22 +191,6 @@ function clean(flowConfig) {
} }
} }
function parseKey(key){
var keys = null;
if(!key){
return null;
}
var index_$ = key.indexOf("$");
var index_dot = key.indexOf(".", 1);
if(index_$ === 0 && index_dot){
keys = [];
keys[0] = key.substring(1,index_dot);
keys[1] = key.substring(index_dot + 1);
keys[0] = keys[0] || "default";
}
return keys;
}
module.exports = { module.exports = {
init: init, init: init,
load: load, load: load,

View File

@ -123,7 +123,7 @@ describe('context', function() {
should.not.exist(context.get("foo")); should.not.exist(context.get("foo"));
}); });
it('enumerates context keys', function() { it.skip('enumerates context keys', function() {
var context = Context.get("1","flowA"); var context = Context.get("1","flowA");
var keys = context.keys(); var keys = context.keys();
@ -163,11 +163,22 @@ describe('context', function() {
config:{} config:{}
} }
}; };
var contextDefaultStorage={
default: "test",
test:{
module: {
init: function() {
return true;
},
get: stubGet,
set: stubSet,
keys: stubKeys,
delete: stubDelete
},
config:{}
}
};
beforeEach(function() {
Context.init({contextStorage:contextStorage});
context = Context.get("1","flow");
});
afterEach(function(done) { afterEach(function(done) {
stubGet.reset(); stubGet.reset();
stubSet.reset(); stubSet.reset();
@ -176,8 +187,15 @@ describe('context', function() {
fs.remove(testDir,done); fs.remove(testDir,done);
}); });
function initializeContext() {
Context.init({contextStorage:contextStorage});
Context.load();
context = Context.get("1","flow");
}
describe('if external context storage exists',function() { describe('if external context storage exists',function() {
it('should store local property to external context storage',function() { it('should store local property to external context storage',function() {
initializeContext();
should.not.exist(context.get("$test.foo")); should.not.exist(context.get("$test.foo"));
context.set("$test.foo","test"); context.set("$test.foo","test");
context.get("$test.foo"); context.get("$test.foo");
@ -187,6 +205,7 @@ describe('context', function() {
stubKeys.called.should.be.true(); stubKeys.called.should.be.true();
}); });
it('should store flow property to external context storage',function() { it('should store flow property to external context storage',function() {
initializeContext();
should.not.exist(context.flow.get("$test.foo")); should.not.exist(context.flow.get("$test.foo"));
context.flow.set("$test.foo","test"); context.flow.set("$test.foo","test");
context.flow.get("$test.foo"); context.flow.get("$test.foo");
@ -196,6 +215,7 @@ describe('context', function() {
stubKeys.called.should.be.true(); stubKeys.called.should.be.true();
}); });
it('should store global property to external context storage',function() { it('should store global property to external context storage',function() {
initializeContext();
should.not.exist(context.global.get("$test.foo")); should.not.exist(context.global.get("$test.foo"));
context.global.set("$test.foo","test"); context.global.set("$test.foo","test");
context.global.get("$test.foo"); context.global.get("$test.foo");
@ -204,37 +224,116 @@ describe('context', function() {
stubSet.called.should.be.true(); stubSet.called.should.be.true();
stubKeys.called.should.be.true(); stubKeys.called.should.be.true();
}); });
it('should store data on default context', function() {
Context.init({contextStorage:contextDefaultStorage});
Context.load();
context = Context.get("1","flow");
should.not.exist(context.get("$nonexist.foo"));
context.set("$nonexist.foo","test");
context.get("$nonexist.foo");
context.keys("$nonexist");
stubGet.called.should.be.true();
stubSet.called.should.be.true();
stubKeys.called.should.be.true();
});
it('should load memory module', function(done) {
Context.init({ contextStorage: { _: {}}});
try {
Context.load();
context.set("_.foo","test");
context.get("$_.foo");
done();
} catch (err) {
done(err);
}
});
it('should load localfilesystem module', function(done) {
Context.init({contextStorage:{ file:{module:"localfilesystem"} }});
try {
Context.load();
done();
} catch (err) {
done(err);
}
});
}); });
describe('if external context storage does not exist',function() { describe('if external context storage does not exist',function() {
it('should store local property to local memory',function() { it('should throw an error using undefined storage for local context', function(done) {
should.not.exist(context.flow.get("$nonexist.foo")); initializeContext();
context.set("$nonexist.foo","test"); try {
context.get("$nonexist.foo").should.eql("test"); context.get("$nonexist.local");
context.keys("$nonexist").should.have.length(1); should.fail(null, null, "An error was not thrown using undefined storage for local context");
stubGet.notCalled.should.be.true(); } catch (err) {
stubSet.notCalled.should.be.true(); if (err.name === "ContextError") {
stubKeys.notCalled.should.be.true(); done();
} else {
done(err);
}
}
}); });
it('should throw an error using undefined storage for flow context', function(done) {
it('should store flow property to local memory',function() { initializeContext();
should.not.exist(context.flow.get("$nonexist.foo")); try {
context.flow.set("$nonexist.foo","test"); context.flow.set("$nonexist.flow");
context.flow.get("$nonexist.foo").should.eql("test"); should.fail(null, null, "An error was not thrown using undefined storage for flow context");
context.flow.keys("$nonexist").should.have.length(1); } catch (err) {
stubGet.notCalled.should.be.true(); if (err.name === "ContextError") {
stubSet.notCalled.should.be.true(); done();
stubKeys.notCalled.should.be.true(); } else {
done(err);
}
}
}); });
it('should fail when using invalid default context', function(done) {
it('should store global property to local memory',function() { Context.init({contextStorage:{default:"noexist"}});
should.not.exist(context.global.get("$nonexist.foo")); try {
context.global.set("$nonexist.foo","test"); Context.load();
context.global.get("$nonexist.foo").should.eql("test"); try {
context.global.keys("$nonexist").should.have.length(1); should.fail(null, null, "An error was not thrown using undefined storage for flow context");
stubGet.notCalled.should.be.true(); } catch (err) {
stubSet.notCalled.should.be.true(); done(err);
stubKeys.notCalled.should.be.true(); }
} catch (err) {
done();
}
});
it('should store data on memory when contextStorage is not defined', function() {
Context.init({});
Context.load();
context = Context.get("1","flow");
context.set("$nonexist.key1", "val1");
context.get("$nonexist.key1").should.eql("val1");
context.flow.set("$nonexist.key2", "val2");
context.flow.get("$nonexist.key2").should.eql("val2");
context.global.set("$nonexist.key1", "val3");
context.global.get("$nonexist.key1").should.eql("val3");
});
it('should fail for the storage with no module', function(done) {
Context.init({ contextStorage: { test: {}}});
try {
Context.load();
try {
should.fail(null, null, "Should fail when no module was specified");
} catch (err) {
done(err);
}
} catch (err) {
done();
}
});
it('should fail to load non-existent module', function(done) {
Context.init({contextStorage:{ file:{module:"nonexistent"} }});
try {
Context.load();
try {
should.fail(null, null, "Should fail to load non-existent module");
} catch (err) {
done(err);
}
} catch (err) {
done();
}
}); });
}); });
}); });
@ -244,17 +343,17 @@ describe('context', function() {
function returnModuleAndKey(input, expectedModule, expectedKey) { function returnModuleAndKey(input, expectedModule, expectedKey) {
var result = parseKey(input); var result = parseKey(input);
result[0].should.eql(expectedModule); result.storage.should.eql(expectedModule);
result[1].should.eql(expectedKey); result.key.should.eql(expectedKey);
}; };
function returnModule(input, expectedModule) { function returnModule(input, expectedModule) {
var result = parseKey(input); var result = parseKey(input);
result[0].should.eql(expectedModule); result.storage.should.eql(expectedModule);
should(result[1]).be.null(); should(result.key).be.null();
}; };
it('should retrun module and key', function() { it('should return module and key', function() {
returnModuleAndKey("$test.aaa","test","aaa"); returnModuleAndKey("$test.aaa","test","aaa");
returnModuleAndKey("$test.aaa.bbb","test","aaa.bbb"); returnModuleAndKey("$test.aaa.bbb","test","aaa.bbb");
returnModuleAndKey("$1.234","1","234"); returnModuleAndKey("$1.234","1","234");
@ -265,23 +364,29 @@ describe('context', function() {
returnModuleAndKey("$test..","test","."); returnModuleAndKey("$test..","test",".");
}); });
// it('should retrun only module', function() { // it('should return only module', function() {
// returnModule("$test","test",null); // returnModule("$test","test",null);
// returnModule("$1","1",null); // returnModule("$1","1",null);
// returnModule("$$test","$test",null); // returnModule("$$test","$test",null);
// returnModule("$test.","test.",null); // returnModule("$test.","test.",null);
// }); // });
it('should retrun module as default', function() { it('should return module as default', function() {
returnModuleAndKey("$default.foo","default","foo"); returnModuleAndKey("$default.foo","default","foo");
returnModuleAndKey("$.foo","default","foo"); returnModuleAndKey("$.foo","default","foo");
// returnModule("$default","default"); // returnModule("$default","default");
// returnModule("$","default"); // returnModule("$","default");
}); });
it('should retrun null', function() { it('should return null', function() {
should(parseKey("test.aaa")).be.null(); var keyPath = parseKey("test.aaa");
should(parseKey("test")).be.null(); keyPath.storage.should.eql("");
keyPath.key.should.eql("test.aaa");
keyPath = parseKey("test");
keyPath.storage.should.eql("");
keyPath.key.should.eql("test");
should(parseKey(null)).be.null(); should(parseKey(null)).be.null();
}); });
}); });