From 2f495ddcacef40c5cbd4d847bfc036d6e093f086 Mon Sep 17 00:00:00 2001 From: Airyzz <36567925+Airyzz@users.noreply.github.com> Date: Sat, 31 Aug 2024 17:43:09 +0930 Subject: [PATCH] Fix parsing invalid sticker, improve error handling --- commet/lib/client/matrix/matrix_client.dart | 4 +- commet/lib/client/matrix/matrix_room.dart | 62 +++++++++++++-------- commet/lib/debug/log.dart | 6 +- 3 files changed, 43 insertions(+), 29 deletions(-) diff --git a/commet/lib/client/matrix/matrix_client.dart b/commet/lib/client/matrix/matrix_client.dart index 05609350..3c8a41df 100644 --- a/commet/lib/client/matrix/matrix_client.dart +++ b/commet/lib/client/matrix/matrix_client.dart @@ -159,8 +159,8 @@ class MatrixClient extends Client { try { await client.init(true, isBackgroundService: isBackgroundService); } catch (error, trace) { - Log.e("Unable to load client $clientName from database"); - Log.onError(error, trace); + Log.onError(error, trace, + content: "Unable to load client $clientName from database"); client.self = ErrorProfile(); manager.alertManager.addAlert(Alert(AlertType.warning, diff --git a/commet/lib/client/matrix/matrix_room.dart b/commet/lib/client/matrix/matrix_room.dart index d02f2e67..6144bc31 100644 --- a/commet/lib/client/matrix/matrix_room.dart +++ b/commet/lib/client/matrix/matrix_room.dart @@ -409,35 +409,49 @@ class MatrixRoom extends Room { TimelineEvent convertEvent(matrix.Event event, {matrix.Timeline? timeline}) { var c = client as MatrixClient; + try { + if (event.redacted) { + return MatrixTimelineEventUnknown(event, client: c); + } - if (event.redacted) { - return MatrixTimelineEventUnknown(event, client: c); - } + if (event.type == matrix.EventTypes.Message) { + if (event.relationshipType == "m.replace") + return MatrixTimelineEventEdit(event, client: c); + if (event.content["chat.commet.type"] == "chat.commet.sticker" && + event.content['url'] is String) + return MatrixTimelineEventSticker(event, client: c); + + if (event.messageType == "m.emote") + return MatrixTimelineEventEmote(event, client: c); - if (event.type == matrix.EventTypes.Message) { - if (event.relationshipType == "m.replace") - return MatrixTimelineEventEdit(event, client: c); - if (event.content["chat.commet.type"] == "chat.commet.sticker") - return MatrixTimelineEventSticker(event, client: c); + return MatrixTimelineEventMessage(event, client: c); + } - if (event.messageType == "m.emote") - return MatrixTimelineEventEmote(event, client: c); + final result = switch (event.type) { + matrix.EventTypes.Sticker => event.content['url'] is String + ? MatrixTimelineEventSticker(event, client: c) + : null, + matrix.EventTypes.Encrypted => + MatrixTimelineEventEncrypted(event, client: c), + matrix.EventTypes.Reaction => + MatrixTimelineEventAddReaction(event, client: c), + matrix.EventTypes.RoomMember => + MatrixTimelineEventMembership(event, client: c), + matrix.EventTypes.Redaction => + MatrixTimelineEventRedaction(event, client: c), + _ => null + }; - return MatrixTimelineEventMessage(event, client: c); + if (result != null) { + return result; + } else { + return MatrixTimelineEventUnknown(event, client: c); + } + } catch (err, trace) { + Log.e("Failed to parse event ${event.eventId} in room ${event.roomId}"); + Log.onError(err, trace, content: "Failed to parse event: ${event.type}"); + return MatrixTimelineEventUnknown(event, client: c); } - - return switch (event.type) { - matrix.EventTypes.Sticker => MatrixTimelineEventSticker(event, client: c), - matrix.EventTypes.Encrypted => - MatrixTimelineEventEncrypted(event, client: c), - matrix.EventTypes.Reaction => - MatrixTimelineEventAddReaction(event, client: c), - matrix.EventTypes.RoomMember => - MatrixTimelineEventMembership(event, client: c), - matrix.EventTypes.Redaction => - MatrixTimelineEventRedaction(event, client: c), - _ => MatrixTimelineEventUnknown(event, client: c) - }; } @override diff --git a/commet/lib/debug/log.dart b/commet/lib/debug/log.dart index 673f996b..40c648f0 100644 --- a/commet/lib/debug/log.dart +++ b/commet/lib/debug/log.dart @@ -129,10 +129,10 @@ class Log { _print(LogEntry(LogType.warning, str)); } - static void onError(Object object, StackTrace trace) { + static void onError(Object object, StackTrace trace, {String? content}) { String? info = getDetailFromStackTrace(trace); - var entry = LogEntryException( - LogType.error, "${object.toString()} ($info)", object, trace); + var entry = LogEntryException(LogType.error, + content ?? "${object.toString()} ($info)", object, trace); _print(entry); }