|
14 | 14 | import org.commcare.connect.network.connectId.parser.ConfirmBackupCodeResponseParser; |
15 | 15 | import org.commcare.connect.network.connectId.parser.PersonalIdApiResponseParser; |
16 | 16 | import org.commcare.connect.network.connectId.parser.StartConfigurationResponseParser; |
| 17 | +import org.commcare.util.LogTypes; |
17 | 18 | import org.javarosa.core.io.StreamsUtil; |
18 | 19 | import org.javarosa.core.services.Logger; |
19 | 20 | import org.json.JSONException; |
20 | 21 | import org.json.JSONObject; |
21 | 22 |
|
| 23 | +import java.io.ByteArrayInputStream; |
22 | 24 | import java.io.IOException; |
23 | 25 | import java.io.InputStream; |
24 | 26 | import java.util.Map; |
@@ -47,10 +49,45 @@ public void processSuccess(int responseCode, InputStream responseData) { |
47 | 49 | onSuccess((T)sessionData); |
48 | 50 | } |
49 | 51 |
|
50 | | - |
| 52 | + @Override |
| 53 | + public void processFailure(int responseCode, InputStream errorResponse, String url) { |
| 54 | + if (!handleErrorCodeIfPresent(errorResponse, sessionData)) { |
| 55 | + super.processFailure(responseCode, null, url); |
| 56 | + } |
| 57 | + } |
51 | 58 | }; |
52 | 59 | } |
53 | 60 |
|
| 61 | + private boolean handleErrorCodeIfPresent(InputStream errorResponse, PersonalIdSessionData sessionData) { |
| 62 | + try { |
| 63 | + if (errorResponse != null) { |
| 64 | + byte[] errorBytes = StreamsUtil.inputStreamToByteArray(errorResponse); |
| 65 | + String jsonStr = new String(errorBytes, java.nio.charset.StandardCharsets.UTF_8); |
| 66 | + JSONObject json = new JSONObject(jsonStr); |
| 67 | + |
| 68 | + String errorCode = json.optString("error_code", ""); |
| 69 | + sessionData.setSessionFailureCode(errorCode); |
| 70 | + if ("LOCKED_ACCOUNT".equalsIgnoreCase(errorCode)) { |
| 71 | + onFailure(PersonalIdOrConnectApiErrorCodes.ACCOUNT_LOCKED_ERROR, null); |
| 72 | + return true; |
| 73 | + } else if ("INTEGRITY_ERROR".equalsIgnoreCase(errorCode)) { |
| 74 | + if (json.has("sub_code")) { |
| 75 | + String subErrorCode = json.optString("sub_code"); |
| 76 | + Logger.log(LogTypes.TYPE_MAINTENANCE, "Integrity error with subcode " + subErrorCode); |
| 77 | + sessionData.setSessionFailureSubcode(subErrorCode); |
| 78 | + onFailure(PersonalIdOrConnectApiErrorCodes.INTEGRITY_ERROR, null); |
| 79 | + } |
| 80 | + return true; |
| 81 | + } |
| 82 | + } |
| 83 | + } catch (Exception e) { |
| 84 | + Logger.exception("Error parsing error_code", e); |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + |
54 | 91 | public void makeStartConfigurationCall(Activity activity, |
55 | 92 | Map<String, String> body, |
56 | 93 | String integrityToken, |
|
0 commit comments