Propagate exception thrown by ContentProvider.canonicalize

Test: atest FrameworksCoreTests:android.content.ContentResolverTest
Fixes: 149184281
Change-Id: Id200748c9d54222aaf669209a43cbaa5675d8331
diff --git a/core/java/android/content/ContentProvider.java b/core/java/android/content/ContentProvider.java
index 31c3232..9cf6569 100644
--- a/core/java/android/content/ContentProvider.java
+++ b/core/java/android/content/ContentProvider.java
@@ -307,19 +307,7 @@
             try {
                 result.putString(ContentResolver.REMOTE_CALLBACK_RESULT, getType(uri));
             } catch (Exception e) {
-                Parcel parcel = Parcel.obtain();
-                try {
-                    try {
-                        parcel.writeException(e);
-                    } catch (Exception ex) {
-                        // getType threw an unparcelable exception. Wrap the message into
-                        // a parcelable exception type
-                        parcel.writeException(new IllegalStateException(e.getMessage()));
-                    }
-                    result.putByteArray(ContentResolver.REMOTE_CALLBACK_ERROR, parcel.marshall());
-                } finally {
-                    parcel.recycle();
-                }
+                putExceptionInBundle(result, ContentResolver.REMOTE_CALLBACK_ERROR, e);
             }
             callback.sendResult(result);
         }
@@ -602,8 +590,12 @@
         public void canonicalizeAsync(String callingPkg, @Nullable String attributionTag, Uri uri,
                 RemoteCallback callback) {
             final Bundle result = new Bundle();
-            result.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT,
-                    canonicalize(callingPkg, attributionTag, uri));
+            try {
+                result.putParcelable(ContentResolver.REMOTE_CALLBACK_RESULT,
+                        canonicalize(callingPkg, attributionTag, uri));
+            } catch (Exception e) {
+                putExceptionInBundle(result, ContentResolver.REMOTE_CALLBACK_ERROR, e);
+            }
             callback.sendResult(result);
         }
 
@@ -717,6 +709,22 @@
 
             return AppOpsManager.MODE_ALLOWED;
         }
+
+        private void putExceptionInBundle(Bundle bundle, String key, Exception e) {
+            Parcel parcel = Parcel.obtain();
+            try {
+                try {
+                    parcel.writeException(e);
+                } catch (Exception ex) {
+                    // getType threw an unparcelable exception. Wrap the message into
+                    // a parcelable exception type
+                    parcel.writeException(new IllegalStateException(e.getMessage()));
+                }
+                bundle.putByteArray(key, parcel.marshall());
+            } finally {
+                parcel.recycle();
+            }
+        }
     }
 
     boolean checkUser(int pid, int uid, Context context) {