Skip to content

Commit

Permalink
[Connect] Only emit main page load errors in the Connect listener (#9772
Browse files Browse the repository at this point in the history
)

* Dont forward errors from non-main requests

* Rename
  • Loading branch information
simond-stripe authored Dec 12, 2024
1 parent f2f6e50 commit 85cd250
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,12 @@ internal class StripeConnectWebViewContainerImpl<Listener : StripeEmbeddedCompon
request: WebResourceRequest,
errorResponse: WebResourceResponse
) {
controller?.onReceivedError(request.url.toString(), errorResponse.statusCode, errorResponse.reasonPhrase)
controller?.onReceivedError(
requestUrl = request.url.toString(),
httpStatusCode = errorResponse.statusCode,
errorMessage = errorResponse.reasonPhrase,
isMainPageLoad = request.isForMainFrame
)
}

override fun onReceivedError(view: WebView, request: WebResourceRequest, error: WebResourceError) {
Expand All @@ -212,7 +217,11 @@ internal class StripeConnectWebViewContainerImpl<Listener : StripeEmbeddedCompon
} else {
null
}
controller?.onReceivedError(request.url.toString(), errorMessage = errorMessage)
controller?.onReceivedError(
requestUrl = request.url.toString(),
errorMessage = errorMessage,
isMainPageLoad = request.isForMainFrame
)
}

override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal class StripeConnectWebViewContainerController<Listener : StripeEmbedded
private val logger: Logger = Logger.getInstance(enableLogging = BuildConfig.DEBUG),
) : DefaultLifecycleObserver {

private val loggerTag = javaClass.simpleName
private val _stateFlow = MutableStateFlow(StripeConnectWebViewContainerState())

/**
Expand All @@ -59,7 +60,12 @@ internal class StripeConnectWebViewContainerController<Listener : StripeEmbedded
updateState { copy(isNativeLoadingIndicatorVisible = !receivedSetOnLoaderStart) }
}

fun onReceivedError(requestUrl: String, httpStatusCode: Int? = null, errorMessage: String? = null) {
fun onReceivedError(
requestUrl: String,
httpStatusCode: Int? = null,
errorMessage: String? = null,
isMainPageLoad: Boolean,
) {
val errorString = buildString {
if (httpStatusCode != null) {
append("Received $httpStatusCode loading $requestUrl")
Expand All @@ -70,7 +76,12 @@ internal class StripeConnectWebViewContainerController<Listener : StripeEmbedded
append(": $errorMessage")
}
}
listener?.onLoadError(RuntimeException(errorString)) // TODO - wrap error better
logger.debug("($loggerTag) $errorString")

// don't send errors for requests that aren't for the main page load
if (isMainPageLoad) {
listener?.onLoadError(RuntimeException(errorString)) // TODO - wrap error better
}
}

fun shouldOverrideUrlLoading(context: Context, request: WebResourceRequest): Boolean {
Expand All @@ -83,11 +94,11 @@ internal class StripeConnectWebViewContainerController<Listener : StripeEmbedded
url.scheme.equals("https", ignoreCase = true) || url.scheme.equals("http", ignoreCase = true)
) {
// open the URL in an external browser for safety and to preserve back navigation
logger.debug("(StripeConnectWebViewClient) Opening URL in external browser: $url")
logger.debug("($loggerTag) Opening URL in external browser: $url")
stripeIntentLauncher.launchSecureExternalWebTab(context, url)
true // block the request since we're opening it in a secure external tab
} else {
logger.debug("(StripeConnectWebViewClient) Opening non-http/https pop-up request: $url")
logger.debug("($loggerTag) Opening non-http/https pop-up request: $url")
if (url.scheme.equals("mailto", ignoreCase = true)) {
stripeIntentLauncher.launchEmailLink(context, url)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,28 @@ class StripeConnectWebViewContainerControllerTest {

@Test
fun `onReceivedError should forward to listener`() = runTest {
controller.onReceivedError("https://stripe.com", 404, "Not Found")
controller.onReceivedError(
requestUrl = "https://stripe.com",
httpStatusCode = 404,
errorMessage = "Not Found",
isMainPageLoad = true
)

verify(listener).onLoadError(any())
}

@Test
fun `onReceivedError should not forward errors outside of main page load to listener`() = runTest {
controller.onReceivedError(
requestUrl = "https://stripe.com",
httpStatusCode = 404,
errorMessage = "Not Found",
isMainPageLoad = false
)

verify(listener, never()).onLoadError(any())
}

@Test
fun `view should update appearance`() = runTest {
val appearances = listOf(Appearance(), Appearance(colors = Colors(primary = Color.CYAN)))
Expand Down

0 comments on commit 85cd250

Please sign in to comment.