From 132cb0d7c85735d5f300514bcf03d246e6c15077 Mon Sep 17 00:00:00 2001 From: Paul Kepinski Date: Mon, 4 Nov 2024 09:11:05 +0100 Subject: [PATCH] Catch and redirect exception in SaneIsolate to main isolate Fixes #17 --- .../sane/lib/src/isolate_messages/exception.dart | 8 ++++++++ packages/sane/lib/src/sane_isolate.dart | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 packages/sane/lib/src/isolate_messages/exception.dart diff --git a/packages/sane/lib/src/isolate_messages/exception.dart b/packages/sane/lib/src/isolate_messages/exception.dart new file mode 100644 index 0000000..802725e --- /dev/null +++ b/packages/sane/lib/src/isolate_messages/exception.dart @@ -0,0 +1,8 @@ +import 'package:sane/src/exceptions.dart'; +import 'package:sane/src/isolate_messages/interface.dart'; + +class ExceptionResponse implements IsolateResponse { + ExceptionResponse({required this.exception}); + + final SaneException exception; +} diff --git a/packages/sane/lib/src/sane_isolate.dart b/packages/sane/lib/src/sane_isolate.dart index 3794dca..f40ef68 100644 --- a/packages/sane/lib/src/sane_isolate.dart +++ b/packages/sane/lib/src/sane_isolate.dart @@ -6,6 +6,7 @@ import 'package:sane/src/isolate_messages/cancel.dart'; import 'package:sane/src/isolate_messages/close.dart'; import 'package:sane/src/isolate_messages/control_button_option.dart'; import 'package:sane/src/isolate_messages/control_option.dart'; +import 'package:sane/src/isolate_messages/exception.dart'; import 'package:sane/src/isolate_messages/exit.dart'; import 'package:sane/src/isolate_messages/get_all_option_descriptors.dart'; import 'package:sane/src/isolate_messages/get_devices.dart'; @@ -57,6 +58,10 @@ class SaneIsolate implements Sane { final response = await replyPort.first; replyPort.close(); + if (response is ExceptionResponse) { + throw response.exception; + } + return response; } @@ -300,9 +305,14 @@ void _isolateEntryPoint(_IsolateEntryPointArgs args) { isolateReceivePort.listen((envellope) async { envellope = envellope as _IsolateMessageEnveloppe; - envellope.replyPort.send( - await envellope.message.handle(sane), - ); + late IsolateResponse response; + try { + response = await envellope.message.handle(sane); + } on SaneException catch (e) { + response = ExceptionResponse(exception: e); + } + + envellope.replyPort.send(response); }); }