Skip to content

Commit

Permalink
Don't log default status code for StripeException (#8229)
Browse files Browse the repository at this point in the history
* Don't log default status code for StripeException

* restrict visibility of DEFAULT_VALUE field
  • Loading branch information
amk-stripe committed Apr 6, 2024
1 parent 2f66b05 commit 0c92dc0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ interface ErrorReporter {
}

fun getAdditionalParamsFromStripeException(stripeException: StripeException): Map<String, String> {
val statusCode =
if (stripeException.statusCode == StripeException.DEFAULT_STATUS_CODE) {
null
} else {
stripeException.statusCode
}
return mapOf(
"analytics_value" to stripeException.analyticsValue(),
"status_code" to stripeException.statusCode.toString(),
"status_code" to statusCode?.toString(),
"request_id" to stripeException.requestId,
"error_type" to stripeException.stripeError?.type,
"error_code" to stripeException.stripeError?.code,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ class RealErrorReporterTest {
fun `RealErrorReporter logs correct info via analyticsRequestExecutor`() {
val exception = StripeException.create(IllegalArgumentException("this arg isn't legal"))
val expectedAnalyticsValue = exception.analyticsValue()
val expectedStatusCode = exception.statusCode.toString()

realErrorReporter.report(ErrorReporter.ExpectedErrorEvent.GET_SAVED_PAYMENT_METHODS_FAILURE, exception)

val executedAnalyticsRequests = analyticsRequestExecutor.getExecutedRequests()
assertThat(executedAnalyticsRequests.size).isEqualTo(1)
val analyticsRequestParams = executedAnalyticsRequests.get(0).params
assertThat(analyticsRequestParams.get("analytics_value")).isEqualTo(expectedAnalyticsValue)
assertThat(analyticsRequestParams.get("status_code")).isEqualTo(expectedStatusCode)
assertThat(analyticsRequestParams.get("request_id")).isNull()
}

Expand All @@ -62,6 +60,7 @@ class RealErrorReporterTest {
assertThat(executedAnalyticsRequests.size).isEqualTo(1)
val analyticsRequestParams = executedAnalyticsRequests.get(0).params
assertThat(analyticsRequestParams.get("analytics_value")).isEqualTo(expectedAnalyticsValue)
assertThat(analyticsRequestParams.get("status_code")).isNotEqualTo(StripeException.DEFAULT_STATUS_CODE)
assertThat(analyticsRequestParams.get("status_code")).isEqualTo(expectedStatusCode)
assertThat(analyticsRequestParams.get("request_id")).isEqualTo(expectedRequestId)
}
Expand Down Expand Up @@ -90,7 +89,6 @@ class RealErrorReporterTest {
fun `RealErrorReporter logs additionalNonPiiParams via analyticsRequestExecutor`() {
val exception = StripeException.create(IllegalArgumentException("this arg isn't legal"))
val expectedAnalyticsValue = exception.analyticsValue()
val expectedStatusCode = exception.statusCode.toString()

realErrorReporter.report(
errorEvent = ErrorReporter.ExpectedErrorEvent.GET_SAVED_PAYMENT_METHODS_FAILURE,
Expand All @@ -102,7 +100,6 @@ class RealErrorReporterTest {
assertThat(executedAnalyticsRequests.size).isEqualTo(1)
val analyticsRequestParams = executedAnalyticsRequests.get(0).params
assertThat(analyticsRequestParams.get("analytics_value")).isEqualTo(expectedAnalyticsValue)
assertThat(analyticsRequestParams.get("status_code")).isEqualTo(expectedStatusCode)
assertThat(analyticsRequestParams.get("request_id")).isNull()
assertThat(analyticsRequestParams.get("foo")).isEqualTo("bar")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,6 @@ class PaymentSheetEventTest {
"request_id" to "request_123",
"error_type" to "network_error",
"error_code" to "error_123",
"status_code" to "0"
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.util.Objects
abstract class StripeException(
val stripeError: StripeError? = null,
val requestId: String? = null,
val statusCode: Int = 0,
val statusCode: Int = DEFAULT_STATUS_CODE,
cause: Throwable? = null,
message: String? = stripeError?.message
) : Exception(message, cause) {
Expand Down Expand Up @@ -50,6 +50,10 @@ abstract class StripeException(

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
companion object {

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
const val DEFAULT_STATUS_CODE = 0

fun create(throwable: Throwable): StripeException {
return when (throwable) {
is StripeException -> throwable
Expand Down

0 comments on commit 0c92dc0

Please sign in to comment.