Skip to content

Commit

Permalink
fix: Handle connection errors with web_socket_channel >= 2.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hacker1024 committed May 7, 2023
1 parent 37bcdce commit e170b70
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:mitmproxy_ri_client/src/entities/api_request.dart';
import 'package:mitmproxy_ri_client/src/entities/message_set.dart';
Expand Down Expand Up @@ -83,8 +84,9 @@ class Client {
_getDefaultMessageSetSettings,
MessageSetProvider handleRequest = _getDefaultMessageSet,
MessageSetProvider handleResponse = _getDefaultMessageSet,
}) : done = _channel.stream.forEach(
(message) async {
}) : done = _channel.ready
.then(
(_) => _channel.stream.forEach((message) async {
// Decode and parse the API request JSON.
final apiRequestJson =
jsonDecode(message as String) as Map<String, dynamic>;
Expand All @@ -103,20 +105,21 @@ class Client {
final apiResponseJson =
jsonEncode((await apiResponseFuture).toJson()..['id'] = id);
_channel.sink.add(apiResponseJson);
},
).catchError(
}),
)
.catchError(
test: (error) =>
onError != null &&
(error is WebSocketChannelException || error is SocketException),
(error, StackTrace stackTrace) {
Object unwrap(Object error) {
if (error is! WebSocketChannelException) return error;
final inner = error.inner;
return inner == null ? error : unwrap(inner);
}

onError!
.call(unwrap(error as WebSocketChannelException), stackTrace);
onError!.call(unwrap(error as Object), stackTrace);
},
test: (error) =>
onError != null && error is WebSocketChannelException,
);

/// Disconnect from the addon.
Expand Down

0 comments on commit e170b70

Please sign in to comment.