From e9b644ddcb97fae29486945d250757e655c0b2b1 Mon Sep 17 00:00:00 2001 From: Eduard Maximovich Date: Wed, 5 Dec 2018 17:55:36 +0300 Subject: [PATCH] more accurate error handling --- app/build.gradle | 6 +- .../bipwallet/internal/ReactiveAdapter.java | 72 ++++++++++--------- 2 files changed, 43 insertions(+), 35 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 56545de3..0647abb0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,8 +39,8 @@ android { applicationId "network.minter.bipwallet" minSdkVersion minterMinSdk targetSdkVersion minterMaxSdk - versionCode 53 - versionName "1.2.3-dev08" + versionCode 54 + versionName "1.2.3-dev09" testInstrumentationRunner "network.minter.bipwallet.internal.WalletTestRunner" vectorDrawables.useSupportLibrary = true multiDexEnabled true @@ -117,7 +117,7 @@ android { } ext { - minterExplorerSDK = "0.2.5" + minterExplorerSDK = "0.2.6" minterProfileSDK = "0.2.1" minterBlockchainSDK = "0.3.1" minterCoreSDK = "0.2.1" diff --git a/app/src/main/java/network/minter/bipwallet/internal/ReactiveAdapter.java b/app/src/main/java/network/minter/bipwallet/internal/ReactiveAdapter.java index d66df6f3..42ac5746 100644 --- a/app/src/main/java/network/minter/bipwallet/internal/ReactiveAdapter.java +++ b/app/src/main/java/network/minter/bipwallet/internal/ReactiveAdapter.java @@ -32,7 +32,6 @@ import com.google.gson.reflect.TypeToken; import java.io.IOException; -import java.util.Collections; import io.reactivex.Observable; import io.reactivex.ObservableSource; @@ -104,16 +103,16 @@ public static ProfileResult createProfileErrorResult(final String json, i ProfileResult out; try { - out = gson.fromJson(json, new TypeToken>() { - }.getType()); + if (json == null || json.isEmpty()) { + out = createProfileEmpty(code, message); + } else { + out = gson.fromJson(json, new TypeToken>() { + }.getType()); + } + } catch (Exception e) { Timber.e(e, "Unable to parse profile error: %s", json); - out = new ProfileResult<>(); - out.error = new ProfileResult.Error(); - out.error.message = String.format("%d %s", message); - out.error.code = String.valueOf(code); - out.error.data = Collections.emptyMap(); - out.data = null; + out = createProfileEmpty(code, message); } return out; @@ -211,14 +210,16 @@ public static BCResult createBcErrorResult(final String json, int code, S BCResult out; try { - out = gson.fromJson(json, new TypeToken>() { - }.getType()); + if (json == null || json.isEmpty()) { + out = createBcEmpty(code, message); + } else { + out = gson.fromJson(json, new TypeToken>() { + }.getType()); + } + } catch (Exception e) { Timber.e(e, "Unable to parse blockchain error: %s", json); - out = new BCResult<>(); - out.code = null; - out.message = String.format("%d %s", code, message); - out.statusCode = code; + out = createBcEmpty(code, message); } return out; @@ -319,15 +320,15 @@ public static BCExplorerResult createBcExpErrorResult(final String json, BCExplorerResult out; try { - out = gson.fromJson(json, new TypeToken>() { - }.getType()); + if (json == null || json.isEmpty()) { + out = createBcExpEmpty(code, message); + } else { + out = gson.fromJson(json, new TypeToken>() { + }.getType()); + } } catch (Exception e) { Timber.e(e, "Unable to parse explorer (blockchain) error: %s", json); - out = new BCExplorerResult<>(); - out.error = new BCExplorerResult.ErrorResult(); - out.error.code = null; - out.error.message = String.format("Bad response: %d %s", code, message); - out.statusCode = 500; + out = createBcExpEmpty(code, message); } return out; @@ -419,14 +420,18 @@ public void onFailure(@NonNull Call call1, @NonNull Throwable t) { }; } - public static ExpResult createExpErrorResult(final String json) { + public static ExpResult createExpErrorResult(final String json, int code, String message) { Gson gson = MinterExplorerApi.getInstance().getGsonBuilder().create(); ExpResult out; try { - out = gson.fromJson(json, new TypeToken>() { - }.getType()); + if (json == null || json.isEmpty()) { + out = createExpEmpty(code, message); + } else { + out = gson.fromJson(json, new TypeToken>() { + }.getType()); + } } catch (Exception e) { - out = new ExpResult<>(); + out = createExpEmpty(code, message); } return out; @@ -441,10 +446,10 @@ public static ExpResult createExpErrorResult(final Response response) errorBodyString = response.errorBody().string(); } catch (IOException e) { Timber.e(e, "Unable to resolve http exception response"); - return createExpEmpty(); + return createExpEmpty(response.code(), response.message()); } - return createExpErrorResult(errorBodyString); + return createExpErrorResult(errorBodyString, response.code(), response.message()); } public static ExpResult createExpErrorResult(final HttpException exception) { @@ -456,13 +461,16 @@ public static ExpResult createExpErrorResult(final HttpException exceptio errorBodyString = ((HttpException) exception).response().errorBody().string(); } catch (IOException e) { Timber.e(e, "Unable to resolve http exception response"); - return createExpEmpty(); + return createExpEmpty(exception.code(), exception.message()); } - return createExpErrorResult(errorBodyString); + return createExpErrorResult(errorBodyString, exception.code(), exception.message()); } - public static ExpResult createExpEmpty() { - return new ExpResult<>(); + public static ExpResult createExpEmpty(int code, String message) { + ExpResult out = new ExpResult<>(); + out.code = code; + out.error = message; + return out; } }