From b0c1bf835e043468d3aed8abfa09b6966fb3260e Mon Sep 17 00:00:00 2001 From: emawby Date: Thu, 10 Aug 2023 10:43:51 -0700 Subject: [PATCH 1/4] Don't crash if login fails --- .../com/onesignal/internal/OneSignalImp.kt | 32 ++++++++++--------- .../backend/impl/InAppBackendService.kt | 2 +- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt index 1796da6a1b..260b79a389 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt @@ -4,6 +4,7 @@ import android.content.Context import com.onesignal.IOneSignal import com.onesignal.common.IDManager import com.onesignal.common.OneSignalUtils +import com.onesignal.common.exceptions.BackendException import com.onesignal.common.modeling.ModelChangeTags import com.onesignal.common.modules.IModule import com.onesignal.common.safeString @@ -253,7 +254,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider { Logging.log(LogLevel.DEBUG, "login(externalId: $externalId, jwtBearerToken: $jwtBearerToken)") if (!isInitialized) { - throw Exception("Must call 'initWithContext' before use") + Logging.log(LogLevel.ERROR, "Must call 'initWithContext' before using Login") } var currentIdentityExternalId: String? = null @@ -305,20 +306,20 @@ internal class OneSignalImp : IOneSignal, IServiceProvider { ) if (!result) { - throw Exception("Could not login user") + Logging.log(LogLevel.ERROR, "Could not login user") + } else { + // enqueue a RefreshUserOperation to pull the user from the backend and refresh the models. + // This is a separate enqueue operation to ensure any outstanding operations that happened + // after the createAndSwitchToNewUser have been executed, and the retrieval will be the + // most up to date reflection of the user. + _operationRepo!!.enqueueAndWait( + RefreshUserOperation( + _configModel!!.appId, + _identityModelStore!!.model.onesignalId, + ), + true, + ) } - - // enqueue a RefreshUserOperation to pull the user from the backend and refresh the models. - // This is a separate enqueue operation to ensure any outstanding operations that happened - // after the createAndSwitchToNewUser have been executed, and the retrieval will be the - // most up to date reflection of the user. - _operationRepo!!.enqueueAndWait( - RefreshUserOperation( - _configModel!!.appId, - _identityModelStore!!.model.onesignalId, - ), - true, - ) } } @@ -326,7 +327,8 @@ internal class OneSignalImp : IOneSignal, IServiceProvider { Logging.log(LogLevel.DEBUG, "logout()") if (!isInitialized) { - throw Exception("Must call 'initWithContext' before use") + Logging.log(LogLevel.ERROR, "Must call 'initWithContext' before using Login") + return } // only allow one login/logout at a time diff --git a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/backend/impl/InAppBackendService.kt b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/backend/impl/InAppBackendService.kt index 3f84f7488e..19ada10644 100644 --- a/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/backend/impl/InAppBackendService.kt +++ b/OneSignalSDK/onesignal/in-app-messages/src/main/java/com/onesignal/inAppMessages/internal/backend/impl/InAppBackendService.kt @@ -41,7 +41,7 @@ internal class InAppBackendService( override suspend fun getIAMData(appId: String, messageId: String, variantId: String?): GetIAMDataResponse { val htmlPath = htmlPathForMessage(messageId, variantId, appId) - ?: throw Exception("variantId not valid: $variantId") + ?: return GetIAMDataResponse(null, false) val response = _httpClient.get(htmlPath, null) From 2b8b41ad006b8305951de9aff77f45bd7736ead5 Mon Sep 17 00:00:00 2001 From: emawby Date: Thu, 10 Aug 2023 10:51:36 -0700 Subject: [PATCH 2/4] Don't throw for bad alias actions --- .../onesignal/user/internal/UserManager.kt | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt index c731947c57..59ae635287 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt @@ -50,11 +50,13 @@ internal open class UserManager( Logging.log(LogLevel.DEBUG, "setAlias(label: $label, id: $id)") if (label.isEmpty()) { - throw Exception("Cannot add empty alias") + Logging.log(LogLevel.ERROR, "Cannot add empty alias") + return } if (label == IdentityConstants.ONESIGNAL_ID) { - throw Exception("Cannot add '${IdentityConstants.ONESIGNAL_ID}' alias") + Logging.log(LogLevel.ERROR, "Cannot add '${IdentityConstants.ONESIGNAL_ID}' alias") + return } _identityModel[label] = id @@ -65,11 +67,13 @@ internal open class UserManager( aliases.forEach { if (it.key.isEmpty()) { - throw Exception("Cannot add empty alias") + Logging.log(LogLevel.ERROR, "Cannot add empty alias") + return } if (it.key == IdentityConstants.ONESIGNAL_ID) { - throw Exception("Cannot add '${IdentityConstants.ONESIGNAL_ID}' alias") + Logging.log(LogLevel.ERROR, "Cannot add '${IdentityConstants.ONESIGNAL_ID}' alias") + return } } @@ -82,11 +86,13 @@ internal open class UserManager( Logging.log(LogLevel.DEBUG, "removeAlias(label: $label)") if (label.isEmpty()) { - throw Exception("Cannot remove empty alias") + Logging.log(LogLevel.ERROR, "Cannot remove empty alias") + return } if (label == IdentityConstants.ONESIGNAL_ID) { - throw Exception("Cannot remove '${IdentityConstants.ONESIGNAL_ID}' alias") + Logging.log(LogLevel.ERROR, "Cannot remove '${IdentityConstants.ONESIGNAL_ID}' alias") + return } _identityModel.remove(label) @@ -97,11 +103,13 @@ internal open class UserManager( labels.forEach { if (it.isEmpty()) { - throw Exception("Cannot remove empty alias") + Logging.log(LogLevel.ERROR, "Cannot remove empty alias") + return } if (it == IdentityConstants.ONESIGNAL_ID) { - throw Exception("Cannot remove '${IdentityConstants.ONESIGNAL_ID}' alias") + Logging.log(LogLevel.ERROR, "Cannot remove '${IdentityConstants.ONESIGNAL_ID}' alias") + return } } From cfd4dc1286ca29a964733a951db2126d25ffce8f Mon Sep 17 00:00:00 2001 From: emawby Date: Thu, 10 Aug 2023 10:56:43 -0700 Subject: [PATCH 3/4] Don't throw for bad email and sms tokens --- .../java/com/onesignal/user/internal/UserManager.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt index 59ae635287..bb1ddcbd4b 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt @@ -122,7 +122,8 @@ internal open class UserManager( Logging.log(LogLevel.DEBUG, "addEmail(email: $email)") if (!OneSignalUtils.isValidEmail(email)) { - throw Exception("Cannot add invalid email address as subscription: $email") + Logging.log(LogLevel.ERROR, "Cannot add invalid email address as subscription: $email") + return } _subscriptionManager.addEmailSubscription(email) @@ -132,7 +133,8 @@ internal open class UserManager( Logging.log(LogLevel.DEBUG, "removeEmail(email: $email)") if (!OneSignalUtils.isValidEmail(email)) { - throw Exception("Cannot remove invalid email address as subscription: $email") + Logging.log(LogLevel.ERROR, "Cannot remove invalid email address as subscription: $email") + return } _subscriptionManager.removeEmailSubscription(email) @@ -142,7 +144,8 @@ internal open class UserManager( Logging.log(LogLevel.DEBUG, "addSms(sms: $sms)") if (!OneSignalUtils.isValidPhoneNumber(sms)) { - throw Exception("Cannot add invalid sms number as subscription: $sms") + Logging.log(LogLevel.ERROR, "Cannot add invalid sms number as subscription: $sms") + return } _subscriptionManager.addSmsSubscription(sms) @@ -152,7 +155,8 @@ internal open class UserManager( Logging.log(LogLevel.DEBUG, "removeSms(sms: $sms)") if (!OneSignalUtils.isValidPhoneNumber(sms)) { - throw Exception("Cannot remove invalid sms number as subscription: $sms") + Logging.log(LogLevel.ERROR, "Cannot remove invalid sms number as subscription: $sms") + return } _subscriptionManager.removeSmsSubscription(sms) From e110ff7f8bdfc9e3cd3c84b8602b4cf27a7255a8 Mon Sep 17 00:00:00 2001 From: emawby Date: Thu, 10 Aug 2023 10:58:38 -0700 Subject: [PATCH 4/4] Don't throw for empty tag keys --- .../java/com/onesignal/user/internal/UserManager.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt index bb1ddcbd4b..28ac15aaa1 100644 --- a/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt +++ b/OneSignalSDK/onesignal/core/src/main/java/com/onesignal/user/internal/UserManager.kt @@ -166,7 +166,8 @@ internal open class UserManager( Logging.log(LogLevel.DEBUG, "setTag(key: $key, value: $value)") if (key.isEmpty()) { - throw Exception("Cannot add tag with empty key") + Logging.log(LogLevel.ERROR, "Cannot add tag with empty key") + return } _propertiesModel.tags[key] = value @@ -177,7 +178,8 @@ internal open class UserManager( tags.forEach { if (it.key.isEmpty()) { - throw Exception("Cannot add tag with empty key") + Logging.log(LogLevel.ERROR, "Cannot add tag with empty key") + return } } @@ -190,7 +192,8 @@ internal open class UserManager( Logging.log(LogLevel.DEBUG, "removeTag(key: $key)") if (key.isEmpty()) { - throw Exception("Cannot remove tag with empty key") + Logging.log(LogLevel.ERROR, "Cannot remove tag with empty key") + return } _propertiesModel.tags.remove(key) @@ -201,7 +204,8 @@ internal open class UserManager( keys.forEach { if (it.isEmpty()) { - throw Exception("Cannot remove tag with empty key") + Logging.log(LogLevel.ERROR, "Cannot remove tag with empty key") + return } }