Skip to content

Stable error handling for loaded Cordova plugins #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions src/common/pluginloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function injectIfNecessary (id, url, onload, onerror) {
if (id in define.moduleMap) {
onload();
} else {
onerror();
onerror('Module not inserted to module map.');
}
}, onerror);
}
Expand Down Expand Up @@ -79,19 +79,49 @@ function onScriptLoadingComplete (moduleList, finishPluginLoading) {
function handlePluginsObject (path, moduleList, finishPluginLoading) {
// Now inject the scripts.
var scriptCounter = moduleList.length;
var modulesWithProblems = [];

if (!scriptCounter) {
finishPluginLoading();
return;
}

function callOnScriptLoadingComplete () {
var loadedModules = moduleList.filter(function (m) {
return modulesWithProblems.indexOf(m.id) === -1;
});
onScriptLoadingComplete(loadedModules, finishPluginLoading);
}

function scriptLoadedCallback () {
if (!--scriptCounter) {
onScriptLoadingComplete(moduleList, finishPluginLoading);
callOnScriptLoadingComplete();
}
}

function scriptLoadedErrorCallback (id, message, source, lineno, colno, error) {
modulesWithProblems.push(id);
if (typeof message !== 'undefined') {
var messageString = message;
if (typeof message !== 'string') {
messageString = JSON.stringify(message);
}
messageString = 'Could not load all functions. Please confirm or restart your application. \n \n' +
'Details: Error while loading module: "' + id + '". Module will be skipped. ' + messageString;
console.error(messageString);
alert(messageString);
}
if (!--scriptCounter) {
callOnScriptLoadingComplete();
}
}

for (var i = 0; i < moduleList.length; i++) {
injectIfNecessary(moduleList[i].id, path + moduleList[i].file, scriptLoadedCallback);
var moduleId = moduleList[i].id;
// bound function to have the module id when the error occurs.
var boundErrorCallback = scriptLoadedErrorCallback.bind(null, moduleId);
injectIfNecessary(moduleId, path + moduleList[i].file,
scriptLoadedCallback, boundErrorCallback);
}
}

Expand Down