Skip to content

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

@lindeer

Description

@lindeer

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions