fix: Catch all root zone exceptions

This commit is contained in:
Sorunome 2020-09-17 12:53:18 +02:00
parent bc8fef4a94
commit 5d5c7fa8b4
No known key found for this signature in database
GPG key ID: B19471D07FC9BE9C

View file

@ -23,12 +23,24 @@ import 'package:pedantic/pedantic.dart';
import '../famedlysdk.dart'; import '../famedlysdk.dart';
import '../matrix_api.dart'; import '../matrix_api.dart';
import '../src/utils/logs.dart';
import 'cross_signing.dart'; import 'cross_signing.dart';
import 'key_manager.dart'; import 'key_manager.dart';
import 'key_verification_manager.dart'; import 'key_verification_manager.dart';
import 'olm_manager.dart'; import 'olm_manager.dart';
import 'ssss.dart'; import 'ssss.dart';
Future<T> _runInRoot<T>(FutureOr<T> 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 { class Encryption {
final Client client; final Client client;
final bool debug; final bool debug;
@ -68,7 +80,7 @@ class Encryption {
} }
void handleDeviceOneTimeKeysCount(Map<String, int> countJson) { void handleDeviceOneTimeKeysCount(Map<String, int> countJson) {
Zone.root.run(() => olmManager.handleDeviceOneTimeKeysCount(countJson)); _runInRoot(() => olmManager.handleDeviceOneTimeKeysCount(countJson));
} }
void onSync() { void onSync() {
@ -84,21 +96,21 @@ class Encryption {
if (['m.room_key_request', 'm.forwarded_room_key'].contains(event.type)) { 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 // "just" room key request things. We don't need these asap, so we handle
// them in the background // them in the background
unawaited(Zone.root.run(() => keyManager.handleToDeviceEvent(event))); unawaited(_runInRoot(() => keyManager.handleToDeviceEvent(event)));
} }
if (event.type.startsWith('m.key.verification.')) { if (event.type.startsWith('m.key.verification.')) {
// some key verification event. No need to handle it now, we can easily // some key verification event. No need to handle it now, we can easily
// do this in the background // do this in the background
unawaited(Zone.root unawaited(
.run(() => keyVerificationManager.handleToDeviceEvent(event))); _runInRoot(() => keyVerificationManager.handleToDeviceEvent(event)));
} }
if (event.type.startsWith('m.secret.')) { if (event.type.startsWith('m.secret.')) {
// some ssss thing. We can do this in the background // 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) { if (event.sender == client.userID) {
// maybe we need to re-try SSSS secrets // 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'] update.content['content']['msgtype']
.startsWith('m.key.verification.'))) { .startsWith('m.key.verification.'))) {
// "just" key verification, no need to do this in sync // "just" key verification, no need to do this in sync
unawaited(Zone.root unawaited(
.run(() => keyVerificationManager.handleEventUpdate(update))); _runInRoot(() => keyVerificationManager.handleEventUpdate(update)));
} }
if (update.content['sender'] == client.userID && if (update.content['sender'] == client.userID &&
!update.content['unsigned'].containsKey('transaction_id')) { !update.content['unsigned'].containsKey('transaction_id')) {
// maybe we need to re-try SSSS secrets // maybe we need to re-try SSSS secrets
unawaited(Zone.root.run(() => ssss.periodicallyRequestMissingCache())); unawaited(_runInRoot(() => ssss.periodicallyRequestMissingCache()));
} }
} }