diff --git a/lib/encryption/encryption.dart b/lib/encryption/encryption.dart index 9775fb1..8c9ee7b 100644 --- a/lib/encryption/encryption.dart +++ b/lib/encryption/encryption.dart @@ -23,12 +23,24 @@ import 'package:pedantic/pedantic.dart'; import '../famedlysdk.dart'; import '../matrix_api.dart'; +import '../src/utils/logs.dart'; import 'cross_signing.dart'; import 'key_manager.dart'; import 'key_verification_manager.dart'; import 'olm_manager.dart'; import 'ssss.dart'; +Future _runInRoot(FutureOr Function() fn) async { + return await Zone.root.run(() async { + try { + return await fn(); + } catch (e, s) { + Logs.error('Error thrown in root zone: ' + e.toString(), s); + } + return null; + }); +} + class Encryption { final Client client; final bool debug; @@ -68,7 +80,7 @@ class Encryption { } void handleDeviceOneTimeKeysCount(Map countJson) { - Zone.root.run(() => olmManager.handleDeviceOneTimeKeysCount(countJson)); + _runInRoot(() => olmManager.handleDeviceOneTimeKeysCount(countJson)); } void onSync() { @@ -84,21 +96,21 @@ class Encryption { if (['m.room_key_request', 'm.forwarded_room_key'].contains(event.type)) { // "just" room key request things. We don't need these asap, so we handle // them in the background - unawaited(Zone.root.run(() => keyManager.handleToDeviceEvent(event))); + unawaited(_runInRoot(() => keyManager.handleToDeviceEvent(event))); } if (event.type.startsWith('m.key.verification.')) { // some key verification event. No need to handle it now, we can easily // do this in the background - unawaited(Zone.root - .run(() => keyVerificationManager.handleToDeviceEvent(event))); + unawaited( + _runInRoot(() => keyVerificationManager.handleToDeviceEvent(event))); } if (event.type.startsWith('m.secret.')) { // some ssss thing. We can do this in the background - unawaited(Zone.root.run(() => ssss.handleToDeviceEvent(event))); + unawaited(_runInRoot(() => ssss.handleToDeviceEvent(event))); } if (event.sender == client.userID) { // maybe we need to re-try SSSS secrets - unawaited(Zone.root.run(() => ssss.periodicallyRequestMissingCache())); + unawaited(_runInRoot(() => ssss.periodicallyRequestMissingCache())); } } @@ -112,13 +124,13 @@ class Encryption { update.content['content']['msgtype'] .startsWith('m.key.verification.'))) { // "just" key verification, no need to do this in sync - unawaited(Zone.root - .run(() => keyVerificationManager.handleEventUpdate(update))); + unawaited( + _runInRoot(() => keyVerificationManager.handleEventUpdate(update))); } if (update.content['sender'] == client.userID && !update.content['unsigned'].containsKey('transaction_id')) { // maybe we need to re-try SSSS secrets - unawaited(Zone.root.run(() => ssss.periodicallyRequestMissingCache())); + unawaited(_runInRoot(() => ssss.periodicallyRequestMissingCache())); } }