fix error handling in models

This commit is contained in:
Henry Hiles 2026-05-18 10:12:53 -04:00
commit 14ec487bbe
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
4 changed files with 23 additions and 10 deletions

View file

@ -230,7 +230,7 @@ class ClientController extends AsyncNotifier<int> {
Future<Paginate> paginate(PaginateRequest request) async => Future<Paginate> paginate(PaginateRequest request) async =>
Paginate.fromJson(await _sendCommand("paginate", request.toJson())); Paginate.fromJson(await _sendCommand("paginate", request.toJson()));
Future<Profile> getProfile(String userId) async => Profile.fromJson({ Future<Profile> getProfile(String userId) async => Profile.fromJsonWithCatch({
...(await _sendCommand("get_profile", {"user_id": userId})), ...(await _sendCommand("get_profile", {"user_id": userId})),
"id": userId, "id": userId,
}); });

View file

@ -1,4 +1,3 @@
import "package:collection/collection.dart";
import "package:nexus/models/content/avatar.dart"; import "package:nexus/models/content/avatar.dart";
import "package:nexus/models/content/canonical_alias.dart"; import "package:nexus/models/content/canonical_alias.dart";
import "package:nexus/models/content/create.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"; import "package:nexus/models/content/topic.dart";
class Content { class Content {
Content(); final String? parseError;
Content({this.parseError});
factory Content.fromJson(Map<String, dynamic> json) => Content(); factory Content.fromJson(Map<String, dynamic> json) => Content();
Map<String, dynamic> toJson() => {}; Map<String, dynamic> toJson() => {};
static Content fromEventJson(Map<String, dynamic> eventJson, String type) => static Content fromEventJson(Map<String, dynamic> eventJson, String type) {
(EventType.values try {
.firstWhereOrNull((eventType) => eventType.type == type) return EventType.values
?.contentFromJson ?? .firstWhere((eventType) => eventType.type == type)
Content.fromJson)(eventJson); .contentFromJson(eventJson);
} catch (error) {
return Content(parseError: error.toString());
}
}
} }
enum EventType { enum EventType {

View file

@ -8,7 +8,7 @@ part "event.g.dart";
Profile? pmpFromJson(Map<String, dynamic>? json) { Profile? pmpFromJson(Map<String, dynamic>? json) {
final pmp = json?["content"]?["com.beeper.per_message_profile"]; final pmp = json?["content"]?["com.beeper.per_message_profile"];
return pmp == null ? null : Profile.fromJson(pmp); return pmp == null ? null : Profile.fromJsonWithCatch(pmp);
} }
@freezed @freezed

View file

@ -13,6 +13,7 @@ Object? readTimezone(Map<dynamic, dynamic> map, _) =>
abstract class Profile with _$Profile { abstract class Profile with _$Profile {
const factory Profile({ const factory Profile({
required String id, required String id,
String? parseError,
Uri? avatarUrl, Uri? avatarUrl,
@JsonKey(name: "displayname") String? displayName, @JsonKey(name: "displayname") String? displayName,
@ -23,8 +24,16 @@ abstract class Profile with _$Profile {
IList<Pronoun> pronouns, IList<Pronoun> pronouns,
}) = _Profile; }) = _Profile;
factory Profile.fromJson(Map<String, Object?> json) => factory Profile.fromJson(Map<String, dynamic> json) =>
_$ProfileFromJson(json); _$ProfileFromJson(json);
factory Profile.fromJsonWithCatch(Map<String, dynamic> json) {
try {
return Profile.fromJson(json);
} catch (error) {
return Profile(id: json["id"], parseError: error.toString());
}
}
} }
@freezed @freezed