From 2f32e8054ddac59150e170fdaf8a601ca00f5989 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 7 Dec 2016 14:47:40 -0800 Subject: [PATCH] Catch and log exceptions thrown in message listeners provided by apps (#3297) FlutterView's host message processing methods are invoked from native code, and the JNI wrappers will abort the process if the Java side has an uncaught exception. --- .../android/io/flutter/view/FlutterView.java | 35 ++++++++++++++----- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/shell/platform/android/io/flutter/view/FlutterView.java b/shell/platform/android/io/flutter/view/FlutterView.java index 3769566ca4c..58467dbf9e2 100644 --- a/shell/platform/android/io/flutter/view/FlutterView.java +++ b/shell/platform/android/io/flutter/view/FlutterView.java @@ -547,18 +547,29 @@ public class FlutterView extends SurfaceView private void handlePlatformMessage(String channel, String message, final int responseId) { OnMessageListener listener = mOnMessageListeners.get(channel); if (listener != null) { - nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, listener.onMessage(this, message)); + String response = null; + try { + response = listener.onMessage(this, message); + } catch (Exception ex) { + Log.e(TAG, "Uncaught exception in message listener", ex); + } + nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, response); return; } OnMessageListenerAsync asyncListener = mAsyncOnMessageListeners.get(channel); if (asyncListener != null) { - asyncListener.onMessage(this, message, new MessageResponse() { - @Override - public void send(String response) { - nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, response); - } - }); + try { + asyncListener.onMessage(this, message, new MessageResponse() { + @Override + public void send(String response) { + nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, response); + } + }); + } catch (Exception ex) { + Log.e(TAG, "Uncaught exception in async message listener", ex); + nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, null); + } return; } @@ -572,8 +583,14 @@ public class FlutterView extends SurfaceView @CalledByNative private void handlePlatformMessageResponse(int responseId, String response) { MessageReplyCallback callback = mPendingResponses.remove(responseId); - if (callback != null) - callback.onReply(response); + if (callback != null) { + try { + callback.onReply(response); + } catch (Exception ex) { + Log.e(TAG, "Uncaught exception in message listener reply", ex); + return; + } + } } @CalledByNative