Unverified Commit 177e99b3 authored by Mikkel Nygaard Ravn's avatar Mikkel Nygaard Ravn Committed by GitHub

Support unpacking platform error events (#12938)

parent 436aa930
...@@ -268,7 +268,7 @@ class EventChannel { ...@@ -268,7 +268,7 @@ class EventChannel {
/// * a decoded data event (possibly null) for each successful event /// * a decoded data event (possibly null) for each successful event
/// received from the platform plugin; /// received from the platform plugin;
/// * an error event containing a [PlatformException] for each error event /// * an error event containing a [PlatformException] for each error event
/// received from the platform plugin; /// received from the platform plugin.
/// ///
/// Errors occurring during stream activation or deactivation are reported /// Errors occurring during stream activation or deactivation are reported
/// through the [FlutterError] facility. Stream activation happens only when /// through the [FlutterError] facility. Stream activation happens only when
...@@ -279,10 +279,15 @@ class EventChannel { ...@@ -279,10 +279,15 @@ class EventChannel {
StreamController<dynamic> controller; StreamController<dynamic> controller;
controller = new StreamController<dynamic>.broadcast(onListen: () async { controller = new StreamController<dynamic>.broadcast(onListen: () async {
BinaryMessages.setMessageHandler(name, (ByteData reply) async { BinaryMessages.setMessageHandler(name, (ByteData reply) async {
if (reply == null) if (reply == null) {
controller.close(); controller.close();
else } else {
controller.add(codec.decodeEnvelope(reply)); try {
controller.add(codec.decodeEnvelope(reply));
} on PlatformException catch (e) {
controller.addError(e);
}
}
}); });
try { try {
await methodChannel.invokeMethod('listen', arguments); await methodChannel.invokeMethod('listen', arguments);
......
...@@ -161,14 +161,14 @@ void main() { ...@@ -161,14 +161,14 @@ void main() {
const MessageCodec<dynamic> jsonMessage = const JSONMessageCodec(); const MessageCodec<dynamic> jsonMessage = const JSONMessageCodec();
const MethodCodec jsonMethod = const JSONMethodCodec(); const MethodCodec jsonMethod = const JSONMethodCodec();
const EventChannel channel = const EventChannel('ch', jsonMethod); const EventChannel channel = const EventChannel('ch', jsonMethod);
void emitEvent(dynamic event) {
BinaryMessages.handlePlatformMessage(
'ch',
event,
(ByteData reply) {},
);
}
test('can receive event stream', () async { test('can receive event stream', () async {
void emitEvent(dynamic event) {
BinaryMessages.handlePlatformMessage(
'ch',
event,
(ByteData reply) {},
);
}
bool canceled = false; bool canceled = false;
BinaryMessages.setMockMessageHandler( BinaryMessages.setMockMessageHandler(
'ch', 'ch',
...@@ -176,13 +176,13 @@ void main() { ...@@ -176,13 +176,13 @@ void main() {
final Map<dynamic, dynamic> methodCall = jsonMessage.decodeMessage(message); final Map<dynamic, dynamic> methodCall = jsonMessage.decodeMessage(message);
if (methodCall['method'] == 'listen') { if (methodCall['method'] == 'listen') {
final String argument = methodCall['args']; final String argument = methodCall['args'];
emitEvent(jsonMessage.encodeMessage(<dynamic>[argument + '1'])); emitEvent(jsonMethod.encodeSuccessEnvelope(argument + '1'));
emitEvent(jsonMessage.encodeMessage(<dynamic>[argument + '2'])); emitEvent(jsonMethod.encodeSuccessEnvelope(argument + '2'));
emitEvent(null); emitEvent(null);
return jsonMessage.encodeMessage(<dynamic>[null]); return jsonMethod.encodeSuccessEnvelope(null);
} else if (methodCall['method'] == 'cancel') { } else if (methodCall['method'] == 'cancel') {
canceled = true; canceled = true;
return jsonMessage.encodeMessage(<dynamic>[null]); return jsonMethod.encodeSuccessEnvelope(null);
} else { } else {
fail('Expected listen or cancel'); fail('Expected listen or cancel');
} }
...@@ -193,5 +193,33 @@ void main() { ...@@ -193,5 +193,33 @@ void main() {
await new Future<Null>.delayed(Duration.ZERO); await new Future<Null>.delayed(Duration.ZERO);
expect(canceled, isTrue); expect(canceled, isTrue);
}); });
test('can receive error event', () async {
BinaryMessages.setMockMessageHandler(
'ch',
(ByteData message) async {
final Map<dynamic, dynamic> methodCall = jsonMessage.decodeMessage(message);
if (methodCall['method'] == 'listen') {
final String argument = methodCall['args'];
emitEvent(jsonMethod.encodeErrorEnvelope(code: '404', message: 'Not Found.', details: argument));
return jsonMethod.encodeSuccessEnvelope(null);
} else if (methodCall['method'] == 'cancel') {
return jsonMethod.encodeSuccessEnvelope(null);
} else {
fail('Expected listen or cancel');
}
},
);
final List<dynamic> events = <dynamic>[];
final List<dynamic> errors = <dynamic>[];
channel.receiveBroadcastStream('hello').listen(events.add, onError: errors.add);
await new Future<Null>.delayed(Duration.ZERO);
expect(events, isEmpty);
expect(errors, hasLength(1));
expect(errors[0], const isInstanceOf<PlatformException>());
final PlatformException error = errors[0];
expect(error.code, '404');
expect(error.message, 'Not Found.');
expect(error.details, 'hello');
});
}); });
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment