Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions monri/src/main/java/com/monri/android/ErrorParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

Expand All @@ -19,6 +20,7 @@ class ErrorParser {
private static final String FIELD_CODE = "code";
private static final String FIELD_DECLINE_CODE = "decline_code";
private static final String FIELD_ERROR = "error";
private static final String FIELD_ERRORS = "errors";
private static final String FIELD_MESSAGE = "message";
private static final String FIELD_PARAM = "param";
private static final String FIELD_TYPE = "type";
Expand All @@ -28,13 +30,26 @@ static MonriError parseError(String rawError) {
MonriError monriError = new MonriError();
try {
JSONObject jsonError = new JSONObject(rawError);
JSONObject errorObject = jsonError.getJSONObject(FIELD_ERROR);
monriError.charge = errorObject.optString(FIELD_CHARGE);
monriError.code = errorObject.optString(FIELD_CODE);
monriError.decline_code = errorObject.optString(FIELD_DECLINE_CODE);
monriError.message = errorObject.optString(FIELD_MESSAGE);
monriError.param = errorObject.optString(FIELD_PARAM);
monriError.type = errorObject.optString(FIELD_TYPE);
if (jsonError.has(FIELD_ERRORS)) {
monriError.message = "";
JSONArray errorArray = jsonError.getJSONArray(FIELD_ERRORS);
for (int i = 0; i < errorArray.length(); ++i) {
monriError.message += errorArray.getString(i);
if (i != 0 && i != errorArray.length() - 1) {
monriError.message += ",";
}
}
} else {
JSONObject errorObject;
errorObject = jsonError.getJSONObject(FIELD_ERROR);
monriError.charge = errorObject.optString(FIELD_CHARGE);
monriError.code = errorObject.optString(FIELD_CODE);
monriError.decline_code = errorObject.optString(FIELD_DECLINE_CODE);
monriError.message = errorObject.optString(FIELD_MESSAGE);
monriError.param = errorObject.optString(FIELD_PARAM);
monriError.type = errorObject.optString(FIELD_TYPE);
}

} catch (JSONException jsonException) {
monriError.message = MALFORMED_RESPONSE_MESSAGE;
}
Expand Down