fix: Database error handling

This commit is contained in:
Christian Pauly 2020-08-26 09:38:14 +02:00
parent 6fbee4ee05
commit 9142dcbeec
2 changed files with 75 additions and 51 deletions

View file

@ -502,7 +502,7 @@ class Client extends MatrixApi {
StreamController.broadcast(); StreamController.broadcast();
/// Synchronization erros are coming here. /// Synchronization erros are coming here.
final StreamController<SyncError> onSyncError = StreamController.broadcast(); final StreamController<SdkError> onSyncError = StreamController.broadcast();
/// Synchronization erros are coming here. /// Synchronization erros are coming here.
final StreamController<ToDeviceEventDecryptionError> onOlmError = final StreamController<ToDeviceEventDecryptionError> onOlmError =
@ -725,7 +725,7 @@ class Client extends MatrixApi {
return; return;
} }
Logs.error('Error during processing events: ' + e.toString(), s); Logs.error('Error during processing events: ' + e.toString(), s);
onSyncError.add(SyncError( onSyncError.add(SdkError(
exception: e is Exception ? e : Exception(e), stackTrace: s)); exception: e is Exception ? e : Exception(e), stackTrace: s));
await Future.delayed(Duration(seconds: syncErrorTimeoutSec), _sync); await Future.delayed(Duration(seconds: syncErrorTimeoutSec), _sync);
} }
@ -1478,8 +1478,8 @@ class Client extends MatrixApi {
} }
} }
class SyncError { class SdkError {
Exception exception; Exception exception;
StackTrace stackTrace; StackTrace stackTrace;
SyncError({this.exception, this.stackTrace}); SdkError({this.exception, this.stackTrace});
} }

View file

@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'package:moor/moor.dart'; import 'package:moor/moor.dart';
@ -6,6 +7,7 @@ import 'package:olm/olm.dart' as olm;
import '../../famedlysdk.dart' as sdk; import '../../famedlysdk.dart' as sdk;
import '../../matrix_api.dart' as api; import '../../matrix_api.dart' as api;
import '../../matrix_api.dart'; import '../../matrix_api.dart';
import '../client.dart';
import '../room.dart'; import '../room.dart';
import '../utils/logs.dart'; import '../utils/logs.dart';
@ -57,12 +59,22 @@ class Database extends _$Database {
int get maxFileSize => 1 * 1024 * 1024; int get maxFileSize => 1 * 1024 * 1024;
/// Update errors are coming here.
final StreamController<SdkError> onError = StreamController.broadcast();
@override @override
MigrationStrategy get migration => MigrationStrategy( MigrationStrategy get migration => MigrationStrategy(
onCreate: (Migrator m) { onCreate: (Migrator m) async {
return m.createAll(); try {
await m.createAll();
} catch (e, s) {
Logs.error(e, s);
onError.add(SdkError(exception: e, stackTrace: s));
rethrow;
}
}, },
onUpgrade: (Migrator m, int from, int to) async { onUpgrade: (Migrator m, int from, int to) async {
try {
// this appears to be only called once, so multiple consecutive upgrades have to be handled appropriately in here // this appears to be only called once, so multiple consecutive upgrades have to be handled appropriately in here
if (from == 1) { if (from == 1) {
await m.createIndexIfNotExists(userDeviceKeysIndex); await m.createIndexIfNotExists(userDeviceKeysIndex);
@ -92,7 +104,8 @@ class Database extends _$Database {
from++; from++;
} }
if (from == 4) { if (from == 4) {
await m.addColumnIfNotExists(olmSessions, olmSessions.lastReceived); await m.addColumnIfNotExists(
olmSessions, olmSessions.lastReceived);
from++; from++;
} }
if (from == 5) { if (from == 5) {
@ -104,8 +117,14 @@ class Database extends _$Database {
inboundGroupSessions, inboundGroupSessions.senderClaimedKeys); inboundGroupSessions, inboundGroupSessions.senderClaimedKeys);
from++; from++;
} }
} catch (e, s) {
Logs.error(e, s);
onError.add(SdkError(exception: e, stackTrace: s));
rethrow;
}
}, },
beforeOpen: (_) async { beforeOpen: (_) async {
try {
if (executor.dialect == SqlDialect.sqlite) { if (executor.dialect == SqlDialect.sqlite) {
final ret = await customSelect('PRAGMA journal_mode=WAL').get(); final ret = await customSelect('PRAGMA journal_mode=WAL').get();
if (ret.isNotEmpty) { if (ret.isNotEmpty) {
@ -113,6 +132,11 @@ class Database extends _$Database {
ret.first.data['journal_mode'].toString()); ret.first.data['journal_mode'].toString());
} }
} }
} catch (e, s) {
Logs.error(e, s);
onError.add(SdkError(exception: e, stackTrace: s));
rethrow;
}
}, },
); );