From af295b2817c0b0ba3a9aaeafa7ce14b94bcb4827 Mon Sep 17 00:00:00 2001 From: ggurdin <46800240+ggurdin@users.noreply.github.com> Date: Wed, 19 Mar 2025 10:31:53 -0400 Subject: [PATCH] chore: better error logging if subscription info is null (#2171) --- lib/pangea/common/utils/error_handler.dart | 2 +- .../controllers/subscription_controller.dart | 8 +++-- .../models/base_subscription_info.dart | 34 ++++++++++++++++--- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/pangea/common/utils/error_handler.dart b/lib/pangea/common/utils/error_handler.dart index c8c8b6483..325f0d0af 100644 --- a/lib/pangea/common/utils/error_handler.dart +++ b/lib/pangea/common/utils/error_handler.dart @@ -66,7 +66,7 @@ class ErrorHandler { // Custom handling for PangeaWarningError debugPrint("PangeaWarningError: ${e.message}"); } else { - if (m != null) debugPrint("error message: $m"); + debugPrint("error message: ${m ?? e}"); } Sentry.addBreadcrumb(Breadcrumb(data: data)); diff --git a/lib/pangea/subscription/controllers/subscription_controller.dart b/lib/pangea/subscription/controllers/subscription_controller.dart index 40effec99..2a93b3310 100644 --- a/lib/pangea/subscription/controllers/subscription_controller.dart +++ b/lib/pangea/subscription/controllers/subscription_controller.dart @@ -146,7 +146,9 @@ class SubscriptionController extends BaseController { e: e, s: s, data: { - "availableSubscriptionInfo": availableSubscriptionInfo?.toJson(), + "availableSubscriptionInfo": availableSubscriptionInfo?.toJson( + validate: false, + ), }, ); } @@ -345,7 +347,9 @@ class SubscriptionController extends BaseController { e: e, s: s, data: { - "availableSubscriptionInfo": availableSubscriptionInfo?.toJson(), + "availableSubscriptionInfo": availableSubscriptionInfo?.toJson( + validate: false, + ), }, ); } diff --git a/lib/pangea/subscription/models/base_subscription_info.dart b/lib/pangea/subscription/models/base_subscription_info.dart index 207ee631e..ded6cbe9b 100644 --- a/lib/pangea/subscription/models/base_subscription_info.dart +++ b/lib/pangea/subscription/models/base_subscription_info.dart @@ -115,10 +115,22 @@ class AvailableSubscriptionsInfo { } Future _cacheSubscriptionInfo() async { - await subscriptionBox.write( - PLocalKey.availableSubscriptionInfo, - toJson(), - ); + try { + final json = toJson(); + await subscriptionBox.write( + PLocalKey.availableSubscriptionInfo, + json, + ); + } catch (e, s) { + ErrorHandler.logError( + e: e, + s: s, + data: { + "appIds": appIds, + "allProducts": allProducts, + }, + ); + } } AvailableSubscriptionsInfo? _getCachedSubscriptionInfo() { @@ -144,6 +156,14 @@ class AvailableSubscriptionsInfo { } factory AvailableSubscriptionsInfo.fromJson(Map json) { + if (!json.containsKey('app_ids') || !json.containsKey('all_products')) { + throw "Cached subscription info is missing required fields"; + } + + if (json['all_products'] is! List || json['app_ids'] is! Map) { + throw "Cached subscription info contains incorrect data type(s)"; + } + final appIds = SubscriptionAppIds.fromJson(json['app_ids']); final allProducts = (json['all_products'] as List) .map((product) => SubscriptionDetails.fromJson(product)) @@ -155,7 +175,11 @@ class AvailableSubscriptionsInfo { ); } - Map toJson() { + Map toJson({validate = true}) { + if (validate && (appIds == null || allProducts == null)) { + throw "appIds or allProducts is null in AvailableSubscriptionsInfo"; + } + final data = {}; data['app_ids'] = appIds?.toJson(); data['all_products'] =