From 679c382106ac3ec90d56ad277205d63978545990 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Fri, 27 Jan 2023 15:54:37 -0600 Subject: [PATCH] pass, catch, and display error dialog from newOrder --- .../sub_widgets/buy_warning_popup.dart | 100 ++++++++++++++---- lib/services/buy/simplex/simplex_api.dart | 4 + 2 files changed, 86 insertions(+), 18 deletions(-) diff --git a/lib/pages/buy_view/sub_widgets/buy_warning_popup.dart b/lib/pages/buy_view/sub_widgets/buy_warning_popup.dart index 082a19306..4b99a66d8 100644 --- a/lib/pages/buy_view/sub_widgets/buy_warning_popup.dart +++ b/lib/pages/buy_view/sub_widgets/buy_warning_popup.dart @@ -29,18 +29,9 @@ class BuyWarningPopup extends StatelessWidget { SimplexOrder? order; Future> newOrder(SimplexQuote quote) async { - final response = await SimplexAPI.instance.newOrder(quote); + final orderResponse = await SimplexAPI.instance.newOrder(quote); - // if (response.value != null) { - // ref.read(simplexProvider).updateOrder(response.value!); - // } else { - // Logging.instance.log( - // "_loadQuote: $response", - // level: LogLevel.Warning, - // ); - // } - - return response; + return orderResponse; } Future> redirect(SimplexOrder order) async { @@ -122,13 +113,86 @@ class BuyWarningPopup extends StatelessWidget { rightButton: PrimaryButton( label: "Continue", onPressed: () async { - BuyResponse order = await newOrder(quote); - await redirect(order.value as SimplexOrder).then((_response) async { - this.order = order.value as SimplexOrder; - Navigator.of(context, rootNavigator: isDesktop).pop(); - Navigator.of(context, rootNavigator: isDesktop).pop(); - await _buyInvoice(); - }); + BuyResponse orderResponse = await newOrder(quote); + if (orderResponse.exception == null) { + await redirect(orderResponse.value as SimplexOrder) + .then((_response) async { + this.order = orderResponse.value as SimplexOrder; + Navigator.of(context, rootNavigator: isDesktop).pop(); + Navigator.of(context, rootNavigator: isDesktop).pop(); + await _buyInvoice(); + }); + } else { + await showDialog( + context: context, + barrierDismissible: true, + builder: (context) { + if (isDesktop) { + return DesktopDialog( + maxWidth: 450, + child: Padding( + padding: const EdgeInsets.all(32), + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + Text( + "Simplex API error", + style: STextStyles.desktopH3(context), + ), + const SizedBox( + height: 24, + ), + Text( + "${orderResponse.exception?.errorMessage}", + style: STextStyles.smallMed14(context), + ), + const SizedBox( + height: 56, + ), + Row( + children: [ + const Spacer(), + Expanded( + child: PrimaryButton( + buttonHeight: ButtonHeight.l, + label: "Ok", + onPressed: Navigator.of(context).pop, + ), + ), + ], + ) + ], + ), + ), + ); + } else { + return StackDialog( + title: "Simplex API error", + message: "${orderResponse.exception?.errorMessage}", + // "${quoteResponse.exception?.errorMessage.substring(8, (quoteResponse.exception?.errorMessage?.length ?? 109) - (8 + 6))}", + rightButton: TextButton( + style: Theme.of(context) + .extension()! + .getSecondaryEnabledButtonStyle(context), + child: Text( + "Ok", + style: STextStyles.button(context).copyWith( + color: Theme.of(context) + .extension()! + .accentColorDark), + ), + onPressed: () { + Navigator.of(context).pop(); + Navigator.of(context).pop(); + Navigator.of(context).pop(); // weee + }, + ), + ); + } + }, + ); + } }, ), icon: SizedBox( diff --git a/lib/services/buy/simplex/simplex_api.dart b/lib/services/buy/simplex/simplex_api.dart index 8b6fdb05f..0b6666fa3 100644 --- a/lib/services/buy/simplex/simplex_api.dart +++ b/lib/services/buy/simplex/simplex_api.dart @@ -273,6 +273,10 @@ class SimplexAPI { throw Exception('newOrder exception: statusCode= ${res.statusCode}'); } final jsonArray = jsonDecode(res.body); // TODO check if valid json + if (jsonArray['error'] == true || jsonArray['error'] == 'true') { + print('error'); + throw Exception(jsonArray['message']); + } SimplexOrder _order = SimplexOrder( quote: quote,