From 0ab96ad6a1828380d9a5f9a940f6d534bc088608 Mon Sep 17 00:00:00 2001 From: julian Date: Fri, 5 May 2023 10:17:20 -0600 Subject: [PATCH] fix: some exchange api error propagation --- .../exchange/change_now/change_now_api.dart | 12 ++++++-- .../exchange/trocador/trocador_api.dart | 28 +++++++++++++++++-- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/services/exchange/change_now/change_now_api.dart b/lib/services/exchange/change_now/change_now_api.dart index 6f050d2e2..e14b2f128 100644 --- a/lib/services/exchange/change_now/change_now_api.dart +++ b/lib/services/exchange/change_now/change_now_api.dart @@ -100,12 +100,18 @@ class ChangeNowAPI { body: jsonEncode(body), ); - final parsed = jsonDecode(response.body); + try { + final parsed = jsonDecode(response.body); - return parsed; + return parsed; + } catch (_) { + Logging.instance.log("ChangeNOW api failed to parse: ${response.body}", + level: LogLevel.Error); + rethrow; + } } catch (e, s) { Logging.instance - .log("_makeRequest($uri) threw: $e\n$s", level: LogLevel.Error); + .log("_makePostRequest($uri) threw: $e\n$s", level: LogLevel.Error); rethrow; } } diff --git a/lib/services/exchange/trocador/trocador_api.dart b/lib/services/exchange/trocador/trocador_api.dart index 317ebba53..d74ff2460 100644 --- a/lib/services/exchange/trocador/trocador_api.dart +++ b/lib/services/exchange/trocador/trocador_api.dart @@ -302,10 +302,32 @@ abstract class TrocadorAPI { final json = await _makeGetRequest(uri); final map = Map.from(json as Map); - return ExchangeResponse(value: TrocadorTradeNew.fromMap(map)); + try { + return ExchangeResponse(value: TrocadorTradeNew.fromMap(map)); + } catch (e, s) { + String error = map["error"] as String? ?? json.toString(); + if (error == + "trade could not be generated, some unknown error happened") { + error = + "This trade couldn't be completed. Please select another provider."; + } + + Logging.instance.log( + "_getNewTrade failed to parse response: $json\n$e\n$s", + level: LogLevel.Error, + ); + return ExchangeResponse( + exception: ExchangeException( + error, + ExchangeExceptionType.serializeResponseError, + ), + ); + } } catch (e, s) { - Logging.instance - .log("_getNewTrade exception: $e\n$s", level: LogLevel.Error); + Logging.instance.log( + "_getNewTrade exception: $e\n$s", + level: LogLevel.Error, + ); return ExchangeResponse( exception: ExchangeException( e.toString(),