From 55f4692d99594ff151c1469fcb29adf2d0e94343 Mon Sep 17 00:00:00 2001 From: Alex Hart Date: Tue, 13 Dec 2022 15:51:45 -0400 Subject: [PATCH] Add logging for response fields when an error happens. --- .../signal/donations/ResponseFieldLogger.kt | 27 +++++++++++ .../java/org/signal/donations/StripeApi.kt | 10 +++-- .../donations/ResponseFieldLoggerTest.kt | 45 +++++++++++++++++++ .../donations/StripeIntentAccessorTest.kt | 3 +- .../donations/StripeSetupIntentTest.kt | 2 +- 5 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 donations/lib/src/main/java/org/signal/donations/ResponseFieldLogger.kt create mode 100644 donations/lib/src/test/java/org/signal/donations/ResponseFieldLoggerTest.kt rename donations/lib/src/test/java/{ => org/signal}/donations/StripeIntentAccessorTest.kt (95%) rename donations/lib/src/test/java/{ => org/signal}/donations/StripeSetupIntentTest.kt (98%) 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 000000000..2a191b16a --- /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 2f626ef7f..8abf56870 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 000000000..f2e2f3df0 --- /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 ab45bee43..4772ed862 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 e6284b99d..948e1e03b 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