title: How to catch "Connection closed while receiving data" from http.send in dart?
body: <p>I want to implement a reconnect logic to the <a href="https://pub.dev/packages/flutter_client_sse" rel="nofollow noreferrer">https://pub.dev/packages/flutter_client_sse</a> package. The http connection stays open and listens to server side events. When the connection is lost for some reason the client should try to reconnect.
  An exception is thrown as soon as the connection is lost but I'm not able to catch it.</p>
  <p>Code for building the connection:</p>
  <pre><code>     try {
          _client = http.Client();
          var request = http.Request(&quot;GET&quot;, Uri.parse(url));
          //Adding headers to the request
          request.headers[&quot;Cache-Control&quot;] = &quot;no-cache&quot;;
          request.headers[&quot;Accept&quot;] = &quot;text/event-stream&quot;;
  
          Future&lt;http.StreamedResponse&gt; response = _client.send(request);
          response.onError((e, s) {
            Log.e(&quot;ERROR!&quot;);
            throw e.cause ?? e;
          });
          response.catchError((e) =&gt; Log.e(&quot;ERROR!&quot;));
          response.asStream().listen((data) {
           ... 
          });
       } catch(e) {
           Log.e(&quot;ERROR!&quot;);
       }   
  </code></pre>
  <p>When the connection is lost an exception is thrown:</p>
  <pre><code>[ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Connection closed while receiving data
  E/flutter (24389): #0      IOClient.send.&lt;anonymous closure&gt; (package:http/src/io_client.dart:49:13)
  E/flutter (24389): #1      _invokeErrorHandler (dart:async/async_error.dart:45:24)
  E/flutter (24389): #2      _HandleErrorStream._handleError (dart:async/stream_pipe.dart:272:9)
  E/flutter (24389): #3      _ForwardingStreamSubscription._handleError (dart:async/stream_pipe.dart:157:13)
  E/flutter (24389): #4      _HttpClientResponse.listen.&lt;anonymous closure&gt; (dart:_http/http_impl.dart:712:16)
  E/flutter (24389): #5      _rootRunBinary (dart:async/zone.dart:1378:47)
  E/flutter (24389): #6      _CustomZone.runBinary (dart:async/zone.dart:1272:19)
  E/flutter (24389): #7      _CustomZone.runBinaryGuarded (dart:async/zone.dart:1178:7)
  </code></pre>
  <p>How can I catch this excption to do the reconnect?</p>