Skip to content

Commit

Permalink
more accurate error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardstock committed Dec 5, 2018
1 parent 3ed951e commit e9b644d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -104,16 +103,16 @@ public static <T> ProfileResult<T> createProfileErrorResult(final String json, i

ProfileResult<T> out;
try {
out = gson.fromJson(json, new TypeToken<ProfileResult<T>>() {
}.getType());
if (json == null || json.isEmpty()) {
out = createProfileEmpty(code, message);
} else {
out = gson.fromJson(json, new TypeToken<ProfileResult<T>>() {
}.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;
Expand Down Expand Up @@ -211,14 +210,16 @@ public static <T> BCResult<T> createBcErrorResult(final String json, int code, S

BCResult<T> out;
try {
out = gson.fromJson(json, new TypeToken<BCResult<T>>() {
}.getType());
if (json == null || json.isEmpty()) {
out = createBcEmpty(code, message);
} else {
out = gson.fromJson(json, new TypeToken<BCResult<T>>() {
}.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;
Expand Down Expand Up @@ -319,15 +320,15 @@ public static <T> BCExplorerResult<T> createBcExpErrorResult(final String json,

BCExplorerResult<T> out;
try {
out = gson.fromJson(json, new TypeToken<BCExplorerResult<T>>() {
}.getType());
if (json == null || json.isEmpty()) {
out = createBcExpEmpty(code, message);
} else {
out = gson.fromJson(json, new TypeToken<BCExplorerResult<T>>() {
}.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;
Expand Down Expand Up @@ -419,14 +420,18 @@ public void onFailure(@NonNull Call<T> call1, @NonNull Throwable t) {
};
}

public static <T> ExpResult<T> createExpErrorResult(final String json) {
public static <T> ExpResult<T> createExpErrorResult(final String json, int code, String message) {
Gson gson = MinterExplorerApi.getInstance().getGsonBuilder().create();
ExpResult<T> out;
try {
out = gson.fromJson(json, new TypeToken<ExpResult<T>>() {
}.getType());
if (json == null || json.isEmpty()) {
out = createExpEmpty(code, message);
} else {
out = gson.fromJson(json, new TypeToken<ExpResult<T>>() {
}.getType());
}
} catch (Exception e) {
out = new ExpResult<>();
out = createExpEmpty(code, message);
}

return out;
Expand All @@ -441,10 +446,10 @@ public static <T> ExpResult<T> createExpErrorResult(final Response<T> 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 <T> ExpResult<T> createExpErrorResult(final HttpException exception) {
Expand All @@ -456,13 +461,16 @@ public static <T> ExpResult<T> 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 <T> ExpResult<T> createExpEmpty() {
return new ExpResult<>();
public static <T> ExpResult<T> createExpEmpty(int code, String message) {
ExpResult<T> out = new ExpResult<>();
out.code = code;
out.error = message;
return out;
}
}

0 comments on commit e9b644d

Please sign in to comment.