Dio: Unable to catch DioError

Created on 16 Jan 2020  ·  13Comments  ·  Source: flutterchina/dio

New Issue Checklist

  • [ Yes ] I have searched for a similar issue in the project and found none

Issue Info

| Info | Value | |
| ------------------------------ | ----------------------------------------------------- | ---- |
| Platform Name | Flutter - Android | |
| Platform Version | 6.0 | |
| Dio Version | e.g. 3.0.7 | |
| Visual Studio Code | VSCode 1.36.0 | |
| Repro rate | 100% | |
| Repro with our demo prj | NA | |
| Demo project link | NA | |

Issue Description and Steps

Code Snippet

try {
      response = await dio.post(
        'https://wendux.github.io/xsddddd',
        data: data,
        onSendProgress: (sent, total) {
          if (total != -1) {
            pr.update(
              progress: (sent / total * 100),
              message: 'Sending File : ' + ((sent / total * 100).toInt()).toStringAsFixed(0) + "%",
            );
            if(sent == total) print('Sent');
          }
        },
        //Handle Exceptions here (Issues in receiving data - memory or else)
        onReceiveProgress: (received, total) {
          if(total != -1) {
            pr.update(
              progress: (received / total * 100),
              message: 'Receiving Data : ' + ((received / total * 100).toInt()).toStringAsFixed(0) + "%",
            );
            if(received == total) print('Received');
          }
        }
      );
    }
    on DioError catch(e) {
      print(e.response.data);
      print(e.response.headers);
      print(e.response.request);
      await showAlertDialog(context,
       alertTitle: 'Internet Connection Error',
        alertText: 'Please check you internet connection & try again',
        numberButtons: 1
      );
      cancelOp = true;

    }

In dispatchRequest, exception being thrown here
DioError err = assureDioError(e, options); throw err;

However it is not caught by the above code snippet

Most helpful comment

Debugging in RELEASE mode is very limited. This is not a valid excuse.

All 13 comments

Same here, can't catch with catch(e) in general, not just on DioError.

It looks like the issue is due to Flutter framework. Many exceptions of several packages are not caught when running the app in DEBUG Mode. However, I noticed that when you run your application in RELEASE Mode, everything seems to work fine (though mostly, & I don't think every time). Waiting for the dio team to share their opinion on it @ffpetrovic

Does latest flutter have this issue? @psgainz

Unfortunately yes!
The only option is to test in RELEASE Mode whenever you feel to test the exception.

Thanks @psgainz
Did you find this somewhere? Is there an issue for this on the flutter repo?

I accidentally encountered it while testing in RELEASE Mode. Also, in some stackoverflow post, I read that this issue also persists for google_sign_in package. Exceptions are not caught when thrown by the package. Only in RELEASE Mode, some exceptions are caught, while some exceptions simply do not occur.

https://stackoverflow.com/questions/51914691/flutter-platform-exception-upon-cancelling-google-sign-in-flow

Yes, I have had this problem with google sign in and also read that release mode fixes it.

Hello everybody! I have a same problem without any "google signin flow", i am just trying to make http request.
Standart dart HttpClient works well.

Closing the issue as the exception works correctly in RELEASE Mode

Debugging in RELEASE mode is very limited. This is not a valid excuse.

a workaround is disable all breakpoints in run tab sidebar, the error will be catched and the app will flow

@talski's workaround is not optimal, as we want to catch all uncaught error if possible to prevent accidental crashes. Any plans at all to fix this?

i am handle in this way on debug mode I know its not good practice
Options(
headers: {'Authorization': 'Bearer $token'},
followRedirects: false,
validateStatus: (status) {
return status <= 500;
}
)

Was this page helpful?
0 / 5 - 0 ratings