Add logging for response fields when an error happens.

main
Alex Hart 2022-12-13 15:51:45 -04:00 zatwierdzone przez Cody Henthorne
rodzic ebe82cf3e6
commit 55f4692d99
5 zmienionych plików z 80 dodań i 7 usunięć

Wyświetl plik

@ -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<Map<String, Any>>() {}
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)
}
}
}

Wyświetl plik

@ -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)
}
}

Wyświetl plik

@ -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())
}
}

Wyświetl plik

@ -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)

Wyświetl plik

@ -1,4 +1,4 @@
package donations
package org.signal.donations
import android.app.Application
import com.fasterxml.jackson.module.kotlin.jsonMapper