Handle errors when initialising library plugin

This commit is contained in:
Nick O'Leary 2021-02-19 14:01:33 +00:00
parent c744af161d
commit c2347076f4
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
2 changed files with 25 additions and 12 deletions

View File

@ -15,7 +15,7 @@
**/
const {events} = require("@node-red/util")
const {events,log} = require("@node-red/util")
const knownTypes = {};
const libraries = {};
const libraryConfigs = {};
@ -38,11 +38,19 @@ function init(runtime) {
runtimeLibraries.forEach(library => {
if (library.type === id) {
library.local = false;
libraryConfigs[library.id] = library;
libraries[library.id] = new plugin.class(library)
libraryConfigs[library.id].type = id;
if (libraries[library.id].init) {
libraries[library.id].init();
try {
libraries[library.id] = new plugin.class(library)
libraryConfigs[library.id] = library;
libraryConfigs[library.id].type = id;
if (libraries[library.id].init) {
libraries[library.id].init().catch(err => {
delete libraries[library.id];
delete libraryConfigs[library.id];
log.warn(log._("library.failedToInit",{library:library.id, error:err.toString()}));
});
}
} catch(err) {
log.warn(log._("library.failedToInit",{library:library.id, error:err.toString()}));
}
}
})
@ -81,26 +89,26 @@ function registerType(id,type) {
function getEntry(library,type,path) {
if (!knownTypes.hasOwnProperty(type)) {
throw new Error(`Unknown library type '${type}'`);
throw new Error(log._("library.unknownType",{type: type}))
}
if (libraries.hasOwnProperty(library)) {
return libraries[library].getEntry(type,path);
} else {
throw new Error(`Unknown library '${library}'`);
throw new Error(log._("library.unknownLibrary",{library: library}))
}
}
function saveEntry(library,type,path,meta,body) {
if (!knownTypes.hasOwnProperty(type)) {
throw new Error(`Unknown library type '${type}'`);
throw new Error(log._("library.unknownType",{type: type}))
}
if (libraries.hasOwnProperty(library)) {
if (libraries[library].saveEntry) {
return libraries[library].saveEntry(type,path,meta,body);
} else {
throw new Error(`Library '${library}' is read-only`);
throw new Error(log._("library.readOnly",{library: library}))
}
} else {
throw new Error(`Unknown library '${library}'`);
throw new Error(log._("library.unknownLibrary",{library: library}))
}
}

View File

@ -89,7 +89,12 @@
"not-available": "Settings not available",
"property-read-only": "Property '__prop__' is read-only"
},
"library": {
"unknownLibrary": "Unknown library: __library__",
"unknownType": "Unknown library type: __type__",
"readOnly": "Library __library__ is read-only",
"failedToInit": "Failed to initialise __library__: __error__"
},
"nodes": {
"credentials": {
"error":"Error loading credentials: __message__",