From 14ec487bbe8d03cc1c82206b32d28d56d0ce76d8 Mon Sep 17 00:00:00 2001 From: Henry-Hiles Date: Mon, 18 May 2026 10:12:53 -0400 Subject: [PATCH] fix error handling in models --- lib/controllers/client_controller.dart | 2 +- lib/models/content/content.dart | 18 +++++++++++------- lib/models/event.dart | 2 +- lib/models/profile.dart | 11 ++++++++++- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/controllers/client_controller.dart b/lib/controllers/client_controller.dart index b09cd1c..612efa6 100644 --- a/lib/controllers/client_controller.dart +++ b/lib/controllers/client_controller.dart @@ -230,7 +230,7 @@ class ClientController extends AsyncNotifier { Future paginate(PaginateRequest request) async => Paginate.fromJson(await _sendCommand("paginate", request.toJson())); - Future getProfile(String userId) async => Profile.fromJson({ + Future getProfile(String userId) async => Profile.fromJsonWithCatch({ ...(await _sendCommand("get_profile", {"user_id": userId})), "id": userId, }); diff --git a/lib/models/content/content.dart b/lib/models/content/content.dart index 5277eeb..dbe3695 100644 --- a/lib/models/content/content.dart +++ b/lib/models/content/content.dart @@ -1,4 +1,3 @@ -import "package:collection/collection.dart"; import "package:nexus/models/content/avatar.dart"; import "package:nexus/models/content/canonical_alias.dart"; import "package:nexus/models/content/create.dart"; @@ -15,16 +14,21 @@ import "package:nexus/models/content/server_acl.dart"; import "package:nexus/models/content/topic.dart"; class Content { - Content(); + final String? parseError; + Content({this.parseError}); factory Content.fromJson(Map json) => Content(); Map toJson() => {}; - static Content fromEventJson(Map eventJson, String type) => - (EventType.values - .firstWhereOrNull((eventType) => eventType.type == type) - ?.contentFromJson ?? - Content.fromJson)(eventJson); + static Content fromEventJson(Map eventJson, String type) { + try { + return EventType.values + .firstWhere((eventType) => eventType.type == type) + .contentFromJson(eventJson); + } catch (error) { + return Content(parseError: error.toString()); + } + } } enum EventType { diff --git a/lib/models/event.dart b/lib/models/event.dart index 58bd388..d77911b 100644 --- a/lib/models/event.dart +++ b/lib/models/event.dart @@ -8,7 +8,7 @@ part "event.g.dart"; Profile? pmpFromJson(Map? json) { final pmp = json?["content"]?["com.beeper.per_message_profile"]; - return pmp == null ? null : Profile.fromJson(pmp); + return pmp == null ? null : Profile.fromJsonWithCatch(pmp); } @freezed diff --git a/lib/models/profile.dart b/lib/models/profile.dart index f0937f7..6ba2656 100644 --- a/lib/models/profile.dart +++ b/lib/models/profile.dart @@ -13,6 +13,7 @@ Object? readTimezone(Map map, _) => abstract class Profile with _$Profile { const factory Profile({ required String id, + String? parseError, Uri? avatarUrl, @JsonKey(name: "displayname") String? displayName, @@ -23,8 +24,16 @@ abstract class Profile with _$Profile { IList pronouns, }) = _Profile; - factory Profile.fromJson(Map json) => + factory Profile.fromJson(Map json) => _$ProfileFromJson(json); + + factory Profile.fromJsonWithCatch(Map json) { + try { + return Profile.fromJson(json); + } catch (error) { + return Profile(id: json["id"], parseError: error.toString()); + } + } } @freezed