From e55042537f475487fdbfd5d2e179d0b0e3906083 Mon Sep 17 00:00:00 2001 From: Yansong Yang Date: Thu, 23 May 2024 11:53:13 -0500 Subject: [PATCH] Catch exceptions when retrieving data from Activity (#669) --- .../mobile/internal/DataMarshallerTests.kt | 24 ++++++++++++++++++- .../mobile/internal/DataMarshaller.kt | 10 +++++--- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/code/core/src/androidTest/java/com/adobe/marketing/mobile/internal/DataMarshallerTests.kt b/code/core/src/androidTest/java/com/adobe/marketing/mobile/internal/DataMarshallerTests.kt index e8f2adcf6..8a6d81ef2 100644 --- a/code/core/src/androidTest/java/com/adobe/marketing/mobile/internal/DataMarshallerTests.kt +++ b/code/core/src/androidTest/java/com/adobe/marketing/mobile/internal/DataMarshallerTests.kt @@ -21,6 +21,7 @@ import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith +import java.io.Serializable @RunWith(AndroidJUnit4::class) class DataMarshallerTests { @@ -176,6 +177,23 @@ class DataMarshallerTests { @Test fun marshalInvalidUrl_NoCrash() { + val throwsException = ObjectThrowsOnToString() + val intent = + Intent(ApplicationProvider.getApplicationContext(), TestActivity::class.java).apply { + putExtra("key", "value") + putExtra("exceptionKey", throwsException) + } + + val activity = activityTestRule.launchActivity(intent) + val result = DataMarshaller.marshal(activity) + assertEquals( + mapOf("key" to "value"), + result + ) + } + + @Test + fun marshal_whenBundleThrowException_NoCrash() { val intent = Intent(ApplicationProvider.getApplicationContext(), TestActivity::class.java).apply { data = Uri.parse("abc:abc") @@ -188,7 +206,11 @@ class DataMarshallerTests { result ) } - + private class ObjectThrowsOnToString : Serializable { + override fun toString(): String { + throw IllegalStateException("This is a test exception") + } + } companion object { const val LEGACY_PUSH_MESSAGE_ID = "adb_m_id" const val PUSH_MESSAGE_ID_KEY = "pushmessageid" diff --git a/code/core/src/main/java/com/adobe/marketing/mobile/internal/DataMarshaller.kt b/code/core/src/main/java/com/adobe/marketing/mobile/internal/DataMarshaller.kt index 6ee449780..293e992b1 100644 --- a/code/core/src/main/java/com/adobe/marketing/mobile/internal/DataMarshaller.kt +++ b/code/core/src/main/java/com/adobe/marketing/mobile/internal/DataMarshaller.kt @@ -70,9 +70,13 @@ internal object DataMarshaller { NOTIFICATION_IDENTIFIER_KEY -> LOCAL_NOTIFICATION_ID_KEY else -> key } - val value = extraBundle[key] - if (value?.toString()?.isNotEmpty() == true) { - marshalledData[newKey] = value + try { + val value = extraBundle[key] + if (value?.toString()?.isNotEmpty() == true) { + marshalledData[newKey] = value + } + } catch (e: Exception) { + Log.error(CoreConstants.LOG_TAG, LOG_TAG, "Failed to retrieve data (key = $key) from Activity, error is: ${e.message}") } }