From 79e313a0c07d6afbb0e8ecb46e10fd432291145c Mon Sep 17 00:00:00 2001 From: Andrew Grieve Date: Thu, 6 Mar 2014 21:25:48 -0500 Subject: [PATCH] Catch uncaught exceptions in from plugins and turn them into error responses. When a plugin throws an unchecked exception, we're not catching it anywhere and so the error callback is not being called. This change adds a try/catch to catch such exceptions. --- framework/src/org/apache/cordova/PluginManager.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/framework/src/org/apache/cordova/PluginManager.java b/framework/src/org/apache/cordova/PluginManager.java index e9795e86..f0957222 100755 --- a/framework/src/org/apache/cordova/PluginManager.java +++ b/framework/src/org/apache/cordova/PluginManager.java @@ -236,22 +236,25 @@ public class PluginManager { app.sendPluginResult(cr, callbackId); return; } + CallbackContext callbackContext = new CallbackContext(callbackId, app); try { - CallbackContext callbackContext = new CallbackContext(callbackId, app); long pluginStartTime = System.currentTimeMillis(); boolean wasValidAction = plugin.execute(action, rawArgs, callbackContext); long duration = System.currentTimeMillis() - pluginStartTime; - + if (duration > SLOW_EXEC_WARNING_THRESHOLD) { Log.w(TAG, "THREAD WARNING: exec() call to " + service + "." + action + " blocked the main thread for " + duration + "ms. Plugin should use CordovaInterface.getThreadPool()."); } if (!wasValidAction) { PluginResult cr = new PluginResult(PluginResult.Status.INVALID_ACTION); - app.sendPluginResult(cr, callbackId); + callbackContext.sendPluginResult(cr); } } catch (JSONException e) { PluginResult cr = new PluginResult(PluginResult.Status.JSON_EXCEPTION); - app.sendPluginResult(cr, callbackId); + callbackContext.sendPluginResult(cr); + } catch (Exception e) { + Log.e(TAG, "Uncaught exception from plugin", e); + callbackContext.error(e.getMessage()); } }