diff options
author | Jonathan Scott <scottjonathan@google.com> | 2021-10-15 15:09:00 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2021-10-15 15:09:00 +0000 |
commit | 265a3eca1141c83f1e7b58c781b3588d91a7c270 (patch) | |
tree | 7dada9629976efaca4537893d3c6841bdee6f692 | |
parent | 7a55f3eae9420e1a005cbc49c883942ca9260bb7 (diff) | |
parent | 7a49de334fab02528ca9ca7d236b17048e8b5566 (diff) | |
download | connectedappssdk-265a3eca1141c83f1e7b58c781b3588d91a7c270.tar.gz |
Properly handle Errors in IPCs. am: 7a49de334f
Original change: https://googleplex-android-review.googlesource.com/c/platform/external/connectedappssdk/+/16047810
Change-Id: If8417ff4d2cbc1273532aa5112df5f1ab5a7c45a
4 files changed, 41 insertions, 6 deletions
diff --git a/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java b/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java index f5264b0..a4f8ef6 100644 --- a/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java +++ b/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/DispatcherGenerator.java @@ -187,6 +187,21 @@ final class DispatcherGenerator { methodCode.addStatement("$T.throwInBackground(e)", BACKGROUND_EXCEPTION_THROWER_CLASSNAME); methodCode.addStatement("return throwableBytes"); + methodCode.nextControlFlow("catch ($T e)", Error.class); + + // parcel is recycled in this method + methodCode.addStatement("$1T throwableParcel = $1T.obtain()", PARCEL_CLASSNAME); + methodCode.add("throwableParcel.writeInt(1); //errors\n"); + methodCode.addStatement( + "$T.writeThrowableToParcel(throwableParcel, e)", PARCEL_UTILITIES_CLASSNAME); + methodCode.addStatement( + "$1T throwableBytes = parcelCallReceiver.prepareResponse(callId, throwableParcel)", + ArrayTypeName.of(byte.class)); + methodCode.addStatement("throwableParcel.recycle()"); + + methodCode.addStatement("$T.throwInBackground(e)", BACKGROUND_EXCEPTION_THROWER_CLASSNAME); + + methodCode.addStatement("return throwableBytes"); methodCode.endControlFlow(); MethodSpec callMethod = diff --git a/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java b/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java index 20bf0fb..53e5c72 100644 --- a/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java +++ b/processor/src/main/java/com/google/android/enterprise/connectedapps/processor/FakeOtherGenerator.java @@ -215,6 +215,8 @@ final class FakeOtherGenerator { methodBuilder.addStatement(methodCall); methodBuilder.nextControlFlow("catch ($T e)", RuntimeException.class); methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME); + methodBuilder.nextControlFlow("catch ($T e)", Error.class); + methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME); methodBuilder.endControlFlow(); classBuilder.addMethod(methodBuilder.build()); } @@ -267,6 +269,8 @@ final class FakeOtherGenerator { methodBuilder.addStatement(methodCall); methodBuilder.nextControlFlow("catch ($T e)", RuntimeException.class); methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME); + methodBuilder.nextControlFlow("catch ($T e)", Error.class); + methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME); methodBuilder.endControlFlow(); classBuilder.addMethod(methodBuilder.build()); @@ -332,6 +336,8 @@ final class FakeOtherGenerator { } methodBuilder.nextControlFlow("catch ($T e)", RuntimeException.class); methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME); + methodBuilder.nextControlFlow("catch ($T e)", Error.class); + methodBuilder.addStatement("throw new $T(e)", PROFILE_RUNTIME_EXCEPTION_CLASSNAME); methodBuilder.endControlFlow(); classBuilder.addMethod(methodBuilder.build()); diff --git a/sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java b/sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java index 6ea9005..511a77a 100644 --- a/sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java +++ b/sdk/src/main/java/com/google/android/enterprise/connectedapps/exceptions/ProfileRuntimeException.java @@ -16,12 +16,12 @@ package com.google.android.enterprise.connectedapps.exceptions; /** - * Thrown when a {@link RuntimeException} is thrown during a cross-profile call. + * Thrown when a {@link Throwable} is thrown during a cross-profile call. * * <p>To get the original exception, call {@link #getCause()}. */ public class ProfileRuntimeException extends RuntimeException { - public ProfileRuntimeException(RuntimeException cause) { + public ProfileRuntimeException(Throwable cause) { super(cause); } } diff --git a/sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java b/sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java index 0999a35..9511e19 100644 --- a/sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java +++ b/sdk/src/main/java/com/google/android/enterprise/connectedapps/internal/BackgroundExceptionThrower.java @@ -24,15 +24,23 @@ public final class BackgroundExceptionThrower { private BackgroundExceptionThrower() {} private static class ThrowingRunnable implements Runnable { - RuntimeException throwable; + RuntimeException runtimeException; + Error error; - ThrowingRunnable(RuntimeException throwable) { - this.throwable = throwable; + ThrowingRunnable(RuntimeException runtimeException) { + this.runtimeException = runtimeException; + } + + ThrowingRunnable(Error error) { + this.error = error; } @Override public void run() { - throw throwable; + if (error != null) { + throw error; + } + throw runtimeException; } } @@ -41,4 +49,10 @@ public final class BackgroundExceptionThrower { // We add a small delay to ensure that the return can be completed before crashing new Handler(Looper.getMainLooper()).postDelayed(new ThrowingRunnable(throwable), 1000); } + + /** Throw the given {@link Error} after a delay on the main looper. */ + public static void throwInBackground(Error throwable) { + // We add a small delay to ensure that the return can be completed before crashing + new Handler(Looper.getMainLooper()).postDelayed(new ThrowingRunnable(throwable), 1000); + } } |