Merge pull request #139 from driftyco/unhandled-reject-error-fix

fix(plugin): handle rejection when Cordova is undefined
This commit is contained in:
Ibrahim Hadeed 2016-04-29 12:10:06 -04:00
commit b1218bd77c

View File

@ -81,17 +81,15 @@ function callCordovaPlugin(pluginObj:any, methodName:string, args:any[], opts:an
// Do this check in here in the case that the Web API for this plugin is available (for example, Geolocation). // Do this check in here in the case that the Web API for this plugin is available (for example, Geolocation).
if(!window.cordova) { if(!window.cordova) {
cordovaWarn(pluginObj.name, methodName); cordovaWarn(pluginObj.name, methodName);
reject && reject({ return {
error: 'cordova_not_available' error: 'cordova_not_available'
}); }
return;
} }
pluginWarn(pluginObj, methodName); pluginWarn(pluginObj, methodName);
reject && reject({ return {
error: 'plugin_not_installed' error: 'plugin_not_installed'
}); };
return;
} }
// console.log('Cordova calling', pluginObj.name, methodName, args); // console.log('Cordova calling', pluginObj.name, methodName, args);
@ -118,14 +116,27 @@ function getPromise(cb) {
} }
function wrapPromise(pluginObj:any, methodName:string, args:any[], opts:any={}) { function wrapPromise(pluginObj:any, methodName:string, args:any[], opts:any={}) {
return getPromise((resolve, reject) => { let pluginResult, rej;
callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject); const p = getPromise((resolve, reject) => {
}) pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, resolve, reject);
rej = reject;
});
// Angular throws an error on unhandled rejection, but in this case we have already printed
// a warning that Cordova is undefined or the plugin is uninstalled, so there is no reason
// to error
if (pluginResult && pluginResult.error) {
p.catch(() => {});
rej(pluginResult.error);
}
return p;
} }
function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any = {}) { function wrapObservable(pluginObj:any, methodName:string, args:any[], opts:any = {}) {
return new Observable(observer => { return new Observable(observer => {
let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer)); let pluginResult = callCordovaPlugin(pluginObj, methodName, args, opts, observer.next.bind(observer), observer.error.bind(observer));
if (pluginResult && pluginResult.error) {
observer.error(pluginResult.error);
}
return () => { return () => {
try { try {