requests icon indicating copy to clipboard operation
requests copied to clipboard

Can't catch SocketException

Open carlhung opened this issue 4 years ago • 10 comments

Exception has occurred.
SocketException (SocketException: OS Error: Connection timed out, errno = 110, address = carlhung.mynetgear.com, port = 45396)

I can catch the returned result. but after a while, the simulator of android will crash with SocketException. my code:

    try {
      request = await Requests.post(
        url + '/login',
        headers: {'content-type': 'application/json'},
        json: dic,
        verify: false,
        // timeoutSeconds: 5,
      );
    } catch (e) {
      return {"result":"Connection Error: $e"}; 
    }

carlhung avatar Jan 13 '20 20:01 carlhung

do you have the stacktrace of where the exception is thrown?

please check in all places you call Requests that you are using await, if not it will cause this behavior https://github.com/dart-lang/http/issues/160

jossef avatar Jan 16 '20 21:01 jossef

I encounter the problem is more like that: https://stackoverflow.com/questions/57182940/unhandled-socket-exception-with-no-internet-connection-when-i-set-timeout-durati

can catch the timeoutException error. then, it will be returned. but after 15-20 sec, it throws socketException.

so, it throws twice. the first exception will be caught.

carlhung avatar Jan 20 '20 06:01 carlhung

can we schedule some time to chat this weekend? mail me your info to [email protected]

jossef avatar Jan 22 '20 08:01 jossef

hey, sorry. Do you use whatsapp? you can add me by my phone number which is +852 64616196 this weekend is chinese new year. You can text me anytime as coronavirus is scaring all hk people(I am from Hong Kong). so, I believe i will be available as no one wants to go out.

carlhung avatar Jan 24 '20 06:01 carlhung

an update on this one? feel like this is an environmental issue.

jossef avatar Apr 01 '20 16:04 jossef

closing due to inactivity. comment to reopen

jossef avatar May 23 '20 16:05 jossef

I am using gsheets api to access a gsheet in my drive. i thought that when the internet is turned off then a pop up would appear on accessingvthe gsheet but am not able to handle the SocketException. https://github.com/dart-lang/http/issues/160 Some peopke have this issue but mine is still unresolved

RisabhKedai avatar Jul 15 '20 17:07 RisabhKedai

Hello, I experience the exact same behavior. I use

import 'package:http/http.dart' as http;
import 'dart:io';

and when

http.Response response = await _client
          .get(dataURL, headers: _standardHeaders)
          .timeout(const Duration(seconds: 20));

runs into a timeout because the server is down

    } on SocketException catch (e) {
      return (Left(e));
    } catch (e) {
      return (Left(e));
    }

catches the timeout exception first, what is perfect!

But then, after some time the client throws

Exception has occurred.
SocketException (SocketException: OS Error: Connection timed out, errno = 110, address = 192.168.178.62, port = 44850)

in io_client.dart at

var ioRequest = (await _inner.openUrl(request.method, request.url))

This is not catched. The Client (Android Emulator) crashes and code execution stops.

akreienbring avatar Sep 19 '20 14:09 akreienbring

@akreienbring thanks for sharing! can you help fixing this?

jossef avatar Oct 01 '20 15:10 jossef

Possible same issue https://stackoverflow.com/questions/66034255/http-get-not-fall-into-than-or-catcherror-block

bubnenkoff avatar Feb 04 '21 07:02 bubnenkoff