diff --git a/donations/lib/src/main/java/org/signal/donations/ResponseFieldLogger.kt b/donations/lib/src/main/java/org/signal/donations/ResponseFieldLogger.kt new file mode 100644 index 0000000000..2a191b16a0 --- /dev/null +++ b/donations/lib/src/main/java/org/signal/donations/ResponseFieldLogger.kt @@ -0,0 +1,27 @@ +package org.signal.donations + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.databind.ObjectMapper +import org.signal.core.util.logging.Log +import java.io.IOException + +internal object ResponseFieldLogger { + + private val TAG = Log.tag(ResponseFieldLogger::class.java) + + fun logFields(objectMapper: ObjectMapper, json: String?) { + if (json == null) { + Log.w(TAG, "Response body was null. No keys to print.") + return + } + + try { + val mapType = object : TypeReference>() {} + val map = objectMapper.readValue(json, mapType) + + Log.w(TAG, "Map keys (${map.size}): ${map.keys.joinToString()}", true) + } catch (e: IOException) { + Log.w(TAG, "Failed to produce key map.", true) + } + } +} \ No newline at end of file diff --git a/donations/lib/src/main/java/org/signal/donations/StripeApi.kt b/donations/lib/src/main/java/org/signal/donations/StripeApi.kt index 2f626ef7f8..8abf56870b 100644 --- a/donations/lib/src/main/java/org/signal/donations/StripeApi.kt +++ b/donations/lib/src/main/java/org/signal/donations/StripeApi.kt @@ -134,10 +134,11 @@ class StripeApi( try { objectMapper.readValue(it.body()!!.string()) } catch (e: InvalidDefinitionException) { + Log.w(TAG, "Failed to parse JSON for StripeSetupIntent.") + ResponseFieldLogger.logFields(objectMapper, it.body()?.string()) throw StripeError.FailedToParseSetupIntentResponseError(e) - } catch (e: StripeError.PostError) { - throw e } catch (e: Exception) { + Log.w(TAG, "Failed to read value from JSON.", e, true) throw StripeError.FailedToParseSetupIntentResponseError(null) } } @@ -154,10 +155,11 @@ class StripeApi( try { objectMapper.readValue(it.body()!!.string()) } catch (e: InvalidDefinitionException) { + Log.w(TAG, "Failed to parse JSON for StripePaymentIntent.") + ResponseFieldLogger.logFields(objectMapper, it.body()?.string()) throw StripeError.FailedToParsePaymentIntentResponseError(e) - } catch (e: StripeError.PostError) { - throw e } catch (e: Exception) { + Log.w(TAG, "Failed to read value from JSON.", e, true) throw StripeError.FailedToParsePaymentIntentResponseError(null) } } diff --git a/donations/lib/src/test/java/org/signal/donations/ResponseFieldLoggerTest.kt b/donations/lib/src/test/java/org/signal/donations/ResponseFieldLoggerTest.kt new file mode 100644 index 0000000000..f2e2f3df08 --- /dev/null +++ b/donations/lib/src/test/java/org/signal/donations/ResponseFieldLoggerTest.kt @@ -0,0 +1,45 @@ +package org.signal.donations + +import com.fasterxml.jackson.databind.ObjectMapper +import org.junit.Before +import org.junit.Test +import org.signal.core.util.logging.Log +import org.signal.core.util.logging.Log.Logger + +class ResponseFieldLoggerTest { + + @Before + fun setUp() { + Log.initialize(object : Logger() { + override fun v(tag: String?, message: String?, t: Throwable?, keepLonger: Boolean) = Unit + override fun d(tag: String?, message: String?, t: Throwable?, keepLonger: Boolean) = Unit + override fun i(tag: String?, message: String?, t: Throwable?, keepLonger: Boolean) = Unit + override fun w(tag: String?, message: String?, t: Throwable?, keepLonger: Boolean) = println(message) + override fun e(tag: String?, message: String?, t: Throwable?, keepLonger: Boolean) = Unit + override fun flush() = Unit + }) + } + + @Test + fun `Given a null, when I logFields, then I expect no crash`() { + ResponseFieldLogger.logFields(ObjectMapper(), null) + } + + @Test + fun `Given empty, when I logFields, then I expect no crash`() { + ResponseFieldLogger.logFields(ObjectMapper(), "{}") + } + + @Test + fun `Given non-empty, when I logFields, then I expect no crash`() { + ResponseFieldLogger.logFields(ObjectMapper(), """ + { + "id": "asdf", + "client_secret": 12345, + "structured_obj": { + "a": "a" + } + } + """.trimIndent()) + } +} \ No newline at end of file diff --git a/donations/lib/src/test/java/donations/StripeIntentAccessorTest.kt b/donations/lib/src/test/java/org/signal/donations/StripeIntentAccessorTest.kt similarity index 95% rename from donations/lib/src/test/java/donations/StripeIntentAccessorTest.kt rename to donations/lib/src/test/java/org/signal/donations/StripeIntentAccessorTest.kt index ab45bee434..4772ed8629 100644 --- a/donations/lib/src/test/java/donations/StripeIntentAccessorTest.kt +++ b/donations/lib/src/test/java/org/signal/donations/StripeIntentAccessorTest.kt @@ -1,4 +1,4 @@ -package donations +package org.signal.donations import android.app.Application import org.junit.Assert.assertEquals @@ -6,7 +6,6 @@ import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config -import org.signal.donations.StripeIntentAccessor @RunWith(RobolectricTestRunner::class) @Config(application = Application::class) diff --git a/donations/lib/src/test/java/donations/StripeSetupIntentTest.kt b/donations/lib/src/test/java/org/signal/donations/StripeSetupIntentTest.kt similarity index 98% rename from donations/lib/src/test/java/donations/StripeSetupIntentTest.kt rename to donations/lib/src/test/java/org/signal/donations/StripeSetupIntentTest.kt index e6284b99db..948e1e03bc 100644 --- a/donations/lib/src/test/java/donations/StripeSetupIntentTest.kt +++ b/donations/lib/src/test/java/org/signal/donations/StripeSetupIntentTest.kt @@ -1,4 +1,4 @@ -package donations +package org.signal.donations import android.app.Application import com.fasterxml.jackson.module.kotlin.jsonMapper