KevinnZou / compose-webview-multiplatform

WebView for JetBrains Compose Multiplatform
https://kevinnzou.github.io/compose-webview-multiplatform/
Apache License 2.0
305 stars 39 forks source link

Error Handling specifically for main page load #129

Open Vaibhav2002 opened 1 month ago

Vaibhav2002 commented 1 month ago

How can i add error handling only for the main page load?

This is the current errors exposed, which says to use OnError callback, but how to pass this callback?

/**
 * A list for errors captured in the last load. Reset when a new page is loaded.
 * Errors could be from any resource (iframe, image, etc.), not just for the main page.
 * For more fine grained control use the OnError callback of the WebView.
 */
val errorsForCurrentRequest: SnapshotStateList<WebViewError> = mutableStateListOf()
whoisjeeva commented 1 month ago
val webviewState = rememberWebViewState(url = "https://www.suyambu.net")

LaunchedEffect(webviewState.errorsForCurrentRequest.toList()) {
    for (err in webviewState.errorsForCurrentRequest) {
        println("Err => ${err.code} | ${err.description}")
    }
}

I disconnected my internet and try to load the page I got the following output.

Err => -106 | Failed to load url: https://www.suyambu.net
ERR_INTERNET_DISCONNECTED

errorForCurrentRequest is updating the list from this function

https://github.com/KevinnZou/compose-webview-multiplatform/blob/af895b60ff6afac255807354327ee1b262574026/webview/src/desktopMain/kotlin/com/multiplatform/webview/web/WebEngineExt.kt#L136C1-L136C12

I don't see any listener that I can bind to webviewstate or navigator tol listen for page load error.

KevinnZou commented 1 month ago

@Vaibhav2002 Thanks for your feedback. I apologize for the confusion. Unfortunately, we do not provide an onError callback for this library, despite the comment being copied over from the Android version. As a result, the only way to access error information is by listening to errorsForCurrentRequest, as @whoisjeeva has done. I'm sorry for any inconvenience this may have caused.