From 67fd9cd00e606b76e12665c7f54f9972469edfaf Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 8 Nov 2022 09:42:21 +0100 Subject: [PATCH] refactor: Improve error handling for no olm session found exception Finding no olm session can happen quiet often when there are dead devices in a room. We do not need to print the whole stacktrace then. --- lib/encryption/olm_manager.dart | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/encryption/olm_manager.dart b/lib/encryption/olm_manager.dart index 4fb7a0bd..0470bdef 100644 --- a/lib/encryption/olm_manager.dart +++ b/lib/encryption/olm_manager.dart @@ -566,6 +566,10 @@ class OlmManager { if (fingerprintKey == null || identityKey == null || !deviceKey.checkJsonSignature(fingerprintKey, userId, deviceId)) { + Logs().w( + 'Skipping invalid device key from $userId:$deviceId', + deviceKey, + ); continue; } Logs().v('[OlmManager] Starting session with $userId:$deviceId'); @@ -591,13 +595,17 @@ class OlmManager { } } + /// Encryptes a ToDeviceMessage for the given device with an existing + /// olm session. + /// Throws `NoOlmSessionFoundException` if there is no olm session with this + /// device and none could be created. Future> encryptToDeviceMessagePayload( DeviceKeys device, String type, Map payload, {bool getFromDb = true}) async { final sess = await getOlmSessions(device.curve25519Key!, getFromDb: getFromDb); if (sess.isEmpty) { - throw ('No olm session found for ${device.userId}:${device.deviceId}'); + throw NoOlmSessionFoundException(device); } final fullPayload = { 'type': type, @@ -653,8 +661,11 @@ class OlmManager { userData[device.deviceId!] = await encryptToDeviceMessagePayload( device, type, payload, getFromDb: false); + } on NoOlmSessionFoundException catch (e) { + Logs().d('[LibOlm] Error encrypting to-device event', e); + continue; } catch (e, s) { - Logs().w('[LibOlm] Error encrypting to-device event', e, s); + Logs().wtf('[LibOlm] Error encrypting to-device event', e, s); continue; } } @@ -706,3 +717,13 @@ class OlmManager { _olmAccount = null; } } + +class NoOlmSessionFoundException implements Exception { + final DeviceKeys device; + + NoOlmSessionFoundException(this.device); + + @override + String toString() => + 'No olm session found for ${device.userId}:${device.deviceId}'; +}