From 7cf7038b9a259d4e081ffc7472f685f2d30e3cf4 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 d9dd78555..0b7cec5a3 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 @@ -254,7 +255,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 @@ -306,20 +307,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, - ) } } @@ -327,7 +328,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 3f84f7488..19ada1064 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 67912a463ed18b8af8a42b1a12636eaf09e7ea5f 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 c731947c5..59ae63528 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 54fb09503c7ef1a595488dde7e9689b011e65554 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 59ae63528..bb1ddcbd4 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 2a8fc52be8d5cae7281c79846ccbeb860b41f2a0 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 bb1ddcbd4..28ac15aaa 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 } }