Add ErrorResponse Interceptor: intercept failed loads across Android and iOS #390
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces an ErrorResponse handling feature that lets library consumers intercept and react to failed page/resource loads in the multiplatform WebView. Apps can observe structured error information and decide whether to stop loading or allow the default platform behavior to continue.
Motivation
Apps often need to:
Previously, there was no unified way to observe and control error handling across platforms. This PR provides a simple, consistent API for that use case.
Key Changes
com.multiplatform.webview.response:ErrorResponse: encapsulates error details (platform-agnostic fields likeerrorCode,description, and the failing URL when available).ErrorResponseInterceptor: callback interface to intercept error responses.ShouldStopLoading: boolean-style contract indicating whether the WebView should stop its default error handling and further loading.WebViewNavigatornow accepts anerrorResponseInterceptorthat will be invoked when the underlying platform reports a load error.AccompanistWebView.ktto forward WebView/Chromium errors into the interceptor.WKNavigationDelegate.ktto forwardWKWebViewnavigation and provisional load errors.ErrorResponseSample.ktshows how to plug in the interceptor and display the error.files/samples/errorResponse.htmlhelps demonstrate a failing navigation.Public API
com.multiplatform.webview.responseinterface ErrorResponseInterceptor { fun onInterceptErrorResponse(response: ErrorResponse, navigator: WebViewNavigator): ShouldStopLoading }data class ErrorResponse( val errorCode: Int, val description: String, val url: String? = null )typealias ShouldStopLoading = BooleanrememberWebViewNavigator(errorResponseInterceptor = …)accepts an interceptor instance.Behavior and Semantics
ErrorResponseand aWebViewNavigatorreference.true→ stop loading and suppress default error handling where possible.false→ allow default platform error behavior to proceed.Usage Example
Sample Screen (included)
sample/shared/src/commonMain/kotlin/com/kevinnzou/sample/ErrorResponseSample.ktdemonstrates:webViewNavigator.loadUrl("http://matthiashennemeyer.com/404-on-android-and-tls-error-on-ios")ErrorResponsein state and displaying a simple overlaytruein the interceptor to stop further loadingPlatform Notes
ErrorResponse. Codes correspond to underlying Android/WebView error codes when applicable.WKNavigationDelegateerror callbacks. Codes correspond toNSErrordomain/code where available.Backward Compatibility
ErrorResponseInterceptortorememberWebViewNavigator.Testing and Verification
trueprevents default error handling where platform allows.Documentation
Limitations and Future Work
Testing Roadmap (follow-up PR)
This repository currently does not have tests. To keep this PR focused, I propose a follow-up PR that:
commonTest,androidUnitTest,iosTest) withkotlin-test.ErrorResponse).