fluttercommunity / flutter_webview_plugin

Community WebView Plugin - Allows Flutter to communicate with a native WebView.
https://pub.dev/packages/flutter_webview_plugin
Other
1.48k stars 930 forks source link

Negative http error codes from onHttpError.listen when website is unreachable #786

Open ben-lotze opened 4 years ago

ben-lotze commented 4 years ago

Is there a documentation about the negative http error codes from onHttpError.listen? The goal is to lead to a custom error page on timeouts.

I simulated a non responding server by loading https://www.google.com:81 which will never finish. In this case, the listener receives http error code -2 on Android, and -1001 on iOS.

Can I rely on those codes to not change?

Demo code:

Widget build(BuildContext context) {
    final plugin = FlutterWebviewPlugin();
    plugin.onHttpError.listen((httpError) {
      print('error code: ${httpError.code}, url: ${httpError.url}');
      if ((Platform.isAndroid && httpError.code == '-2') ||
          (Platform.isIOS && httpError.code == '-1001')) {
        // navigate to custom error screen
        // not sure if error codes are stable/reliable
      }
    });
    plugin.onStateChanged.listen((state) {
      print('state changed: type: ${state.type}, url: ${state.url}');
    });

    return WebviewScaffold(
      initialChild: Center(
        child: CircularProgressIndicator(),
      ),
      url: 'https://www.google.com:81',
      hidden: true,
    );
  }

Console outputs and behaviour differ depending on the platform:

Android:

state changed: type: WebViewState.startLoad, url: https://www.google.com:81/
error code: -2, url: https://www.google.com:81/
state changed: type: WebViewState.finishLoad, url: https://www.google.com:81/

Android finishes with a website telling about the timeout:

Screenshot 2020-07-22 at 18 32 56

iOS:

state changed: type: WebViewState.shouldStart, url: https://www.google.com:81/
state changed: type: WebViewState.startLoad, url: https://www.google.com:81/
error code: -1001, url: ?, runtime type: WebViewHttpError

iOS never finishes loading and shows the progress indicator forever.

ben-lotze commented 4 years ago

According to https://nshipster.com/nserror/#nsurlerrordomain-cfnetworkerrors the -1001 on iOS means connection timeout. So, I guess it is safe to take this error code as reliable (as long as it keeps getting forwarded)?

But I cannot find anything about http error -2 on Android.

shinriyo commented 3 years ago

What is -2? it is magic number! do you have const or enum values?

afsalnaaz commented 3 years ago

I have facing same problem. :(

afsalnaaz commented 3 years ago

@ben-lotze are you fix this issue?