From b24f2cd4a92cfacb18253ad71dd4fc5f2e1c741b Mon Sep 17 00:00:00 2001 From: Poovamraj T T Date: Tue, 1 Aug 2023 09:52:21 +0200 Subject: [PATCH] Handle security exception inside the thread block --- .../android/provider/CustomTabsController.java | 15 ++++++--------- .../provider/CustomTabsControllerTest.java | 7 ++++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java b/auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java index 835007bb..ce136d21 100644 --- a/auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java +++ b/auth0/src/main/java/com/auth0/android/provider/CustomTabsController.java @@ -117,13 +117,8 @@ public void launchUri(@NonNull final Uri uri, final boolean launchAsTwa, final R Log.v(TAG, "Custom Tab Context was no longer valid."); return; } - Thread.UncaughtExceptionHandler exceptionHandler = (th, ex) -> { - AuthenticationException e = new AuthenticationException( - "a0.browser_not_available", "Error launching browser for authentication", new Exception(ex)); - CommonThreadSwitcher.getInstance().mainThread(() -> failureCallback.apply(e)); - }; - Thread thread = new Thread(() -> { + new Thread(() -> { try { if (launchAsTwa) { this.launchedAsTwa = true; @@ -139,10 +134,12 @@ public void launchUri(@NonNull final Uri uri, final boolean launchAsTwa, final R } } catch (ActivityNotFoundException ex) { Log.e(TAG, "Could not find any Browser application installed in this device to handle the intent."); + } catch (SecurityException ex) { + AuthenticationException e = new AuthenticationException( + "a0.browser_not_available", "Error launching browser for authentication", ex); + CommonThreadSwitcher.getInstance().mainThread(() -> failureCallback.apply(e)); } - }); - thread.setUncaughtExceptionHandler(exceptionHandler); - thread.start(); + }).start(); } private void launchAsDefault(Context context, Uri uri) { diff --git a/auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java b/auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java index 207d6c08..e66d70b3 100644 --- a/auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java +++ b/auth0/src/test/java/com/auth0/android/provider/CustomTabsControllerTest.java @@ -38,6 +38,7 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.Is.isA; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.eq; @@ -50,6 +51,7 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import com.auth0.android.authentication.AuthenticationException; import com.google.androidbrowserhelper.trusted.TwaLauncher; import com.google.androidbrowserhelper.trusted.splashscreens.SplashScreenStrategy; @@ -305,7 +307,10 @@ public void shouldThrowExceptionIfFailedToLaunchBecauseOfException() { doThrow(e) .when(context).startActivity(any(Intent.class)); controller.launchUri(uri, false, (ex) -> { - assertThat(ex, is(e)); + assertThat(ex, isA(AuthenticationException.class)); + assertThat(ex.getCause(), is(eq(e))); + assertThat(ex.getCode(), is("a0.browser_not_available")); + assertThat(ex.getDescription(), is("Error launching browser for authentication")); assertThat(Looper.myLooper(), is(Looper.getMainLooper())); }); }