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. Project: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/repo Commit: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/commit/6e3e68d9 Tree: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/tree/6e3e68d9 Diff: http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/diff/6e3e68d9 Branch: refs/heads/master Commit: 6e3e68d9cc435643c659d6e5c074d483025e4513 Parents: 85067e4 Author: Andrew Grieve <[email protected]> Authored: Thu Mar 6 21:25:48 2014 -0500 Committer: Archana Naik <[email protected]> Committed: Mon Mar 10 12:21:39 2014 -0700 ---------------------------------------------------------------------- framework/src/org/apache/cordova/PluginManager.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cordova-amazon-fireos/blob/6e3e68d9/framework/src/org/apache/cordova/PluginManager.java ---------------------------------------------------------------------- diff --git a/framework/src/org/apache/cordova/PluginManager.java b/framework/src/org/apache/cordova/PluginManager.java index 0c52542..a7489c1 100755 --- a/framework/src/org/apache/cordova/PluginManager.java +++ b/framework/src/org/apache/cordova/PluginManager.java @@ -245,22 +245,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()); } }
