-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dio is not catching SocketException: Failed host lookup #1869
Comments
UPDATE ISo I studied the code, added some debug points and changed one code line. Then I can say the trouble is Here:
If the future wrapped onto
Now, the error is propagated down (badly) and app is not crashing: Now, the problem is that I'm not returning the appropiate object type on NOTE that this bug (!?) may be found on other parts of the library. And as my logic can encompass, all kind of error must be catchable by end user. I had this problem with other Dart libraries and their owners had difficulties to understand this simple point. I hope you are not the same case. ** Not really propagating down, but at least catching it |
I think I broke this in a recent change for the request aborting. Will take a look later. |
As far as I can tell, this behaves the same way as it has before. I am not saying this is good or ideal but then it is not a bug and we need to discuss if and how we want to change this in a breaking release. Do you have a different experience in a <5.2.0 version? If so, I need a reproducible sample. |
Do you mean with "behave" to not be able to catch any possible error, that make the app crash, by the final user? If so, it is clearly unacceptable. If you know a way to catch these errors, then no breaking release is needed. They are mostly OS errors that other HTTP libraries like Dart HttpClient (re)throw so the final client is able to catch it. As I understand Dio is not (re)throwing some errors like Host Lookup so the only way is app crashing. Let's take a think over there because it simply unacceptable.
No, I have not tried this before. I'm not saying that it has to be changed this or that. Only the simple fact that all these errors have to be a way to catch them. It is simple logic |
I opened this linked issue on CancelableOperation home Repo. |
If you have a reproducible case, please try on 5.1.x - the use of the If this causes your "app" to crash, maybe you can try to register an uncaught exception handler via |
I can confirm this is happening, it's really easy to reproduce for me just try to connect to a non existing host. |
The following test is successful: test(
'SocketException on request',
() async {
final dio = Dio(BaseOptions()..baseUrl = 'https://does.not.exist')
..httpClientAdapter = IOHttpClientAdapter();
await expectLater(
dio.get(
'/test',
data: 'test',
),
throwsA(allOf([
isA<DioException>(),
(DioException e) => e.type == DioExceptionType.unknown,
(DioException e) => e.error is SocketException,
(DioException e) => (e.error as SocketException)
.message
.startsWith("Failed host lookup: 'does.not.exist'"),
(DioException e) =>
e.stackTrace.toString().contains('test/stacktrace_test.dart'),
])),
);
},
testOn: 'vm',
); |
I will try with 5.1.x version. Also I will make a simple test with UPDATE: Here is the test:
No more questions your honor Update:Also not working using |
This comment was marked as abuse.
This comment was marked as abuse.
There are no notifications when you update a comment, so I didn't even see that you posted anything new. Secondly I am on vacation. It would be great if you can provide a PR with a failing test case, then someone else on the contributors team may be able to provide a fix. I only have a phone. |
@kuhnroyal apologizes. having a bad day. You ask me for a PR showing a failing case test. I just show you a simple test with 3 methods that shows that CancelableOperation breaks the error chain. So any future wrapped in a CancelableOperation just can't be catched if it throws an error. As simple as that. I put again this test.
|
@AlexV525 you are right: I deactivate VSCode "Uncught Exepctions" and now it works as desirable. I don't know why? I always use VSCode and it were only stopping on unhandled exceptions. It's weird. Also when I was debugging with my Android mobile device it was stopping with Failed host lookup... It's very weird, compared to the behaviour while executing I will retry the test on Mobile Device with this new VSCode configuration and report if it works as expected |
VSCode related issues: |
@busslina You may need to follow the error handling article of Flutter to see if they're uncaught in your apps. Prints and logs are sometimes less informative when telling where they came from. Closing as the issue is not related to the plugin itself. Please provide more information if anyone is requesting to reopen it or submit a new issue if new wrong behavior was found. |
It's alright, just try to be nice. We are all giving out best here. I also added tests for this in #1875 |
To avoid this from the library side, we might need to wait until we bump the min Dart version to 2.17 in the next major version, which can catch those exceptions and rethrow them using |
This comment was marked as off-topic.
This comment was marked as off-topic.
@YiannisBourkelis from my experience I can tell you this is a VSCode issue. Whenever it happens you must click on the continue debugging execution button on VSCode and it will work as expected. On the compilated app it will not happen |
Try to go back to flutter 3.10.6 |
@busslina @alexcode9 thank you for your replies. I will work for now with unchecked unhandled exceptions on vscode and I hope it will get fixed in a while. |
Yep this is specific to Flutter 3.13.1 and not actually an issue in Dio. |
So is this a bug in the Flutter plugin for Vscode and Android studio? It seems like it hasn't been fixed yet |
I think they traced it back to the dart plugin for VS Code. They seem to only be fixing specific cases but the error in Dio hasn't been covered yet. |
This comment was marked as off-topic.
This comment was marked as off-topic.
me too |
Do you expect to receive something else instead? |
This is an uncaught exception |
You can catch it with https://api.flutter.dev/flutter/dart-async/runZonedGuarded.html |
Do you have any reproducible example? Because this test is green: test('GET wrong port', () async {
final dio = Dio()..options.baseUrl = 'https://httpbun.com:45645/';
await expectLater(
dio.get(
'/get',
),
throwsA(
isA<DioError>()
.having(
(e) => e.type,
'type',
equals(DioErrorType.connectionError),
)
.having(
(e) => e.error,
'error',
isA<SocketException>().having(
(s) => s.message,
'message',
equals(
'Connection refused',
)),
),
),
);
}); |
Here is my example (linux) The main part in: and only after that i can handle it in 'DioErrorHandler' |
Due to a known Dart issue with the debugger, we, unfortunately, have to lock this as we cannot take further action about it and it's not related to dio. Please refer to the above comments to track if the Dart team managed to solve this issue. More specifically: |
Package
dio
Version
5.2.0+1
Output of
flutter doctor -v
Dart Version
3.0.0
Steps to Reproduce
I'm executing a 300 Mb file download on my physical mobile device and interrupting it at some point (maybe at 20%) by switching on the air-plane mode. It works okay, it try to resume download after 5 seconds delay. Then the problem occurs. When executing get method again,
SocketException: Failed host lookup
get thrown. The normal behaviour should be to be catched by try/catch clause, but it isn't. As I can see in the Stack Trace I think it is due to an asynchronous gap not being handled (catched). Key part is around ([07]:) sectionExpected Result
See "Steps to Reproduce"
Actual Result
SocketException (SocketException: Failed host lookup: 'xxxxxxxxxxxx.com' (OS Error: No address associated with hostname, errno = 7))
The text was updated successfully, but these errors were encountered: