http icon indicating copy to clipboard operation
http copied to clipboard

How to catch the exception thrown by 'ClientTransportConnection.terminate'?

Open lindeer opened this issue 2 years ago • 0 comments

my code is a little different from the big await loop in example:

final req = transport.makeRequest(
    headers.entries.map((e) => Header.ascii(e.key, e.value)).toList(growable: false),
    endStream: false,
  );
req.sendData(utf8.encode(body), endStream: true);
final incoming = req.incomingMessages.asBroadcastStream();
final dataStream = incoming.whereType<DataStreamMessage>().map((msg) => utf8.decode(msg.bytes));
final headerStream = incoming.whereType<HeadersStreamMessage>().expand((e) => e.headers);
final detecting = await dataStream.take(10).join('');
if (somecondition(detecting)) {
  await transport.terminate();
  return;
}
await for (final str in dataStream) {
}

but an exception is thrown when running 'somecondition' branch:

Unhandled exception:
HTTP/2 error: Connection error: Connection is being forcefully terminated. (errorCode: 0)

I added a try-catch and ,but it was not working:

try {
  await  transport.terminate();
} catch (e) {
}
await transport.terminate().catchError((e, s) {
}

How could I catch it?

lindeer avatar Jun 01 '23 06:06 lindeer