Skip to content

Commit

Permalink
Catch and redirect exception in SaneIsolate to main isolate (#20)
Browse files Browse the repository at this point in the history
* Catch and redirect exception in SaneIsolate to main isolate
Fixes #17

* Redirect stack trace
  • Loading branch information
Jupi007 authored Nov 4, 2024
1 parent 98643a9 commit 4e783f2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
12 changes: 12 additions & 0 deletions packages/sane/lib/src/isolate_messages/exception.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:sane/src/exceptions.dart';
import 'package:sane/src/isolate_messages/interface.dart';

class ExceptionResponse implements IsolateResponse {
ExceptionResponse({
required this.exception,
required this.stackTrace,
});

final SaneException exception;
final StackTrace stackTrace;
}
22 changes: 19 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,13 @@ class SaneIsolate implements Sane {
final response = await replyPort.first;
replyPort.close();

if (response is ExceptionResponse) {
Error.throwWithStackTrace(
response.exception,
response.stackTrace,
);
}

return response;
}

Expand Down Expand Up @@ -300,9 +308,17 @@ 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 (exception, stackTrace) {
response = ExceptionResponse(
exception: exception,
stackTrace: stackTrace,
);
}

envellope.replyPort.send(response);
});
}

Expand Down

0 comments on commit 4e783f2

Please sign in to comment.