Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch and redirect exception in SaneIsolate to main isolate #20

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/sane/lib/src/isolate_messages/exception.dart
Original file line number Diff line number Diff line change
@@ -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;
}
16 changes: 13 additions & 3 deletions packages/sane/lib/src/sane_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -57,6 +58,10 @@ class SaneIsolate implements Sane {
final response = await replyPort.first;
replyPort.close();

if (response is ExceptionResponse) {
throw response.exception;
Jupi007 marked this conversation as resolved.
Show resolved Hide resolved
}

return response;
}

Expand Down Expand Up @@ -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);
});
}

Expand Down