From b0ee0c63d8c45e14722b38c90eb0b4be35915b91 Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Sun, 16 Apr 2023 18:46:45 +0200 Subject: [PATCH] Catch MSAL exceptions Close #320 --- .../msservices/MicrosoftGraphApiHelper.kt | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/libs/ms-services/src/main/java/de/mm20/launcher2/msservices/MicrosoftGraphApiHelper.kt b/libs/ms-services/src/main/java/de/mm20/launcher2/msservices/MicrosoftGraphApiHelper.kt index 0723e590..9a0e10b9 100644 --- a/libs/ms-services/src/main/java/de/mm20/launcher2/msservices/MicrosoftGraphApiHelper.kt +++ b/libs/ms-services/src/main/java/de/mm20/launcher2/msservices/MicrosoftGraphApiHelper.kt @@ -3,6 +3,7 @@ package de.mm20.launcher2.msservices import android.app.Activity import android.content.Context import android.util.Log +import android.widget.Toast import androidx.core.content.edit import com.azure.core.credential.AccessToken import com.azure.core.credential.TokenCredential @@ -43,13 +44,18 @@ class MicrosoftGraphApiHelper(val context: Context) { if (resId == 0) return null if (clientApplication == null) { clientApplication = withContext(Dispatchers.IO) { - PublicClientApplication.createSingleAccountPublicClientApplication( - context.applicationContext, - resId - ) + try { + PublicClientApplication.createSingleAccountPublicClientApplication( + context.applicationContext, + resId + ) + } catch (e: MsalClientException) { + CrashReporter.logException(e) + null + } } } - return clientApplication!! + return clientApplication } private suspend fun acquireAccessToken(): Boolean { @@ -72,7 +78,11 @@ class MicrosoftGraphApiHelper(val context: Context) { } suspend fun login(context: Activity) { - val clientApplication = getClientApplication() ?: return + val clientApplication = getClientApplication() ?: return run { + withContext(Dispatchers.Main) { + Toast.makeText(context, "Something went wrong, please check the logs", Toast.LENGTH_LONG).show() + } + } suspendCoroutine { clientApplication.signIn(context, "", SCOPES, object : AuthenticationCallback { override fun onSuccess(authenticationResult: IAuthenticationResult?) {