gracefully catch errors when parsing membership state

This commit is contained in:
Henry Hiles 2026-10-12 10:36:41 -04:00
commit 30a5fa100c
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
3 changed files with 9 additions and 14 deletions

View file

@ -17,11 +17,7 @@ import "package:nexus/models/content/topic.dart";
import "package:nexus/models/content/sticker.dart"; import "package:nexus/models/content/sticker.dart";
import "package:nexus/models/content/history_visibility.dart"; import "package:nexus/models/content/history_visibility.dart";
class Content { class const Content({final String? parseError, final StackTrace? stackTrace}) {
final Error? parseError;
const Content({this.parseError});
factory Content.fromJson(Map<String, dynamic> json) => const Content(); factory Content.fromJson(Map<String, dynamic> json) => const Content();
Map<String, dynamic> toJson() => {}; Map<String, dynamic> toJson() => {};
@ -35,9 +31,8 @@ class Content {
.firstWhereOrNull((eventType) => eventType.type == type) .firstWhereOrNull((eventType) => eventType.type == type)
?.contentFromJson ?? ?.contentFromJson ??
Content.fromJson)(json); Content.fromJson)(json);
} catch (error) { } catch (error, stackTrace) {
if (error is Error) return Content(parseError: error); return Content(parseError: error.toString(), stackTrace: stackTrace);
rethrow;
} }
} }
} }
@ -65,5 +60,5 @@ enum EventType(
name("m.room.name", NameContent.fromJson), name("m.room.name", NameContent.fromJson),
reaction("m.reaction", ReactionContent.fromJson), reaction("m.reaction", ReactionContent.fromJson),
pinnedEvents("m.room.pinned_events", PinnedEventsContent.fromJson), pinnedEvents("m.room.pinned_events", PinnedEventsContent.fromJson),
message("m.room.message", MessageContent.fromJson); message("m.room.message", MessageContent.fromJson),
} }

View file

@ -58,16 +58,16 @@ class const Event({
replyTo: replyToFromJson(getContentFromJson(json)), replyTo: replyToFromJson(getContentFromJson(json)),
pmp: json["content"]?["com.beeper.per_message_profile"] == null pmp: json["content"]?["com.beeper.per_message_profile"] == null
? null ? null
: Profile.fromJsonWithCatch( : .fromJsonWithCatch(
json["content"]?["com.beeper.per_message_profile"], json["content"]?["com.beeper.per_message_profile"],
), ),
content: Content.fromEventJson( content: .fromEventJson(
getContentFromJson(json), getContentFromJson(json),
json["decrypted_type"] ?? json["type"], json["decrypted_type"] ?? json["type"],
), ),
previousContent: json["unsigned"]?["prev_content"] == null previousContent: json["unsigned"]?["prev_content"] == null
? null ? null
: Content.fromEventJson( : .fromEventJson(
json["unsigned"]?["prev_content"], json["unsigned"]?["prev_content"],
json["decrypted_type"] ?? json["type"], json["decrypted_type"] ?? json["type"],
), ),

View file

@ -84,11 +84,11 @@ class const EventRenderer(
final child = event.redactedBy != null || event.relationType == "m.replace" final child = event.redactedBy != null || event.relationType == "m.replace"
? null ? null
: switch (event.content) { : switch (event.content) {
Content(:final parseError?) => Row( Content(:final parseError?, :final stackTrace) => Row(
children: [ children: [
ErrorDialog( ErrorDialog(
"An error occurred while parsing event ${event.eventId}:\n$parseError", "An error occurred while parsing event ${event.eventId}:\n$parseError",
parseError.stackTrace, stackTrace,
), ),
], ],
), ),