Skip to content

Commit

Permalink
Add new exception type for mdns
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma committed Nov 14, 2023
1 parent 60782fa commit 3ac6b83
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
9 changes: 0 additions & 9 deletions lib/src/errors.dart

This file was deleted.

19 changes: 19 additions & 0 deletions lib/src/exceptions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// Error thrown with connection is lost
class ConnectionLostException implements Exception {
final String? message;

const ConnectionLostException([this.message]);

@override
String toString() => message ?? 'ConnectionLostError';
}

/// Error thrown when mDNS fails to find a match on the local network
class NotLocalAddressException implements Exception {
final String address;

const NotLocalAddressException(this.address);

@override
String toString() => 'Address not on local network';
}
6 changes: 4 additions & 2 deletions lib/src/rpc/dial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:grpc/grpc_connection_interface.dart';
import 'package:grpc/grpc_or_grpcweb.dart';
import 'package:logger/logger.dart';

import '../exceptions.dart';
import '../gen/proto/rpc/v1/auth.pb.dart' as pb;
import '../gen/proto/rpc/v1/auth.pbgrpc.dart';
import '../gen/proto/rpc/webrtc/v1/signaling.pbgrpc.dart';
Expand Down Expand Up @@ -123,6 +124,8 @@ Future<ClientChannelBase> dial(String address, DialOptions? options, String Func
.._usingMdns = true
..authEntity = address;
return _dialWebRtc(mdnsUri, dialOptsCopy, sessionCallback);
} on NotLocalAddressException catch (e) {
_logger.d(e.toString());
} catch (e) {
_logger.d('Error dialing with mDNS; falling back to other methods', error: e);
}
Expand Down Expand Up @@ -158,8 +161,7 @@ Future<String> searchMdns(String address) async {
}

await discovery.stop();

throw Exception('Address not on local network');
throw NotLocalAddressException(address);
}

Future<ClientChannelBase> _dialDirectGrpc(String address, DialOptions options, String Function() sessionCallback) async {
Expand Down
4 changes: 2 additions & 2 deletions lib/src/rpc/web_rtc/web_rtc_transport_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:grpc/grpc.dart';
import 'package:grpc/grpc_connection_interface.dart';

import '../../errors.dart';
import '../../exceptions.dart';
import '../../gen/proto/rpc/webrtc/v1/grpc.pb.dart' as grpc;
import 'web_rtc_client.dart';

Expand Down Expand Up @@ -63,7 +63,7 @@ class WebRtcTransportStream extends GrpcTransportStream {
if (connectionState == RTCPeerConnectionState.RTCPeerConnectionStateFailed ||
connectionState == RTCPeerConnectionState.RTCPeerConnectionStateDisconnected) {
onRequestFailure(
const ConnectionLostError('RTCPeerConnection lost'),
const ConnectionLostException('RTCPeerConnection lost'),
StackTrace.current,
);
return;
Expand Down

0 comments on commit 3ac6b83

Please sign in to comment.