Utilities for correctly handling json parsing errors on network responses.

master
Alan Evans 2020-10-09 17:11:19 -03:00 committed by GitHub
parent 597d16f566
commit 07b0d8cf6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 52 additions and 59 deletions

View File

@ -404,12 +404,7 @@ public class PushServiceSocket {
Response response = makeServiceRequest(String.format(MESSAGE_PATH, ""), "GET", (RequestBody) null, NO_HEADERS, NO_HANDLER, Optional.absent()); Response response = makeServiceRequest(String.format(MESSAGE_PATH, ""), "GET", (RequestBody) null, NO_HEADERS, NO_HANDLER, Optional.absent());
validateServiceResponse(response); validateServiceResponse(response);
List<SignalServiceEnvelopeEntity> envelopes; List<SignalServiceEnvelopeEntity> envelopes = readBodyJson(response.body(), SignalServiceEnvelopeEntityList.class).getMessages();
try {
envelopes = JsonUtil.fromJson(readBodyString(response.body()), SignalServiceEnvelopeEntityList.class).getMessages();
} catch (IOException e) {
throw new PushNetworkException(e);
}
long serverDeliveredTimestamp = 0; long serverDeliveredTimestamp = 0;
try { try {
@ -1346,11 +1341,7 @@ public class PushServiceSocket {
@Override @Override
public void onResponse(Call call, Response response) { public void onResponse(Call call, Response response) {
try (ResponseBody body = validateServiceResponse(response).body()) { try (ResponseBody body = validateServiceResponse(response).body()) {
try { bodyFuture.set(readBodyString(body));
bodyFuture.set(readBodyString(body));
} catch (IOException e) {
throw new PushNetworkException(e);
}
} catch (IOException e) { } catch (IOException e) {
bodyFuture.setException(e); bodyFuture.setException(e);
} }
@ -1392,9 +1383,8 @@ public class PushServiceSocket {
} }
private Response validateServiceResponse(Response response) throws NonSuccessfulResponseCodeException, PushNetworkException { private Response validateServiceResponse(Response response) throws NonSuccessfulResponseCodeException, PushNetworkException {
int responseCode = response.code(); int responseCode = response.code();
String responseMessage = response.message(); String responseMessage = response.message();
ResponseBody responseBody = response.body();
switch (responseCode) { switch (responseCode) {
case 413: case 413:
@ -1405,58 +1395,23 @@ public class PushServiceSocket {
case 404: case 404:
throw new NotFoundException("Not found"); throw new NotFoundException("Not found");
case 409: case 409:
MismatchedDevices mismatchedDevices; MismatchedDevices mismatchedDevices = readResponseJson(response, MismatchedDevices.class);
try {
mismatchedDevices = JsonUtil.fromJson(readBodyString(responseBody), MismatchedDevices.class);
} catch (JsonProcessingException e) {
Log.w(TAG, e);
throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
} catch (IOException e) {
throw new PushNetworkException(e);
}
throw new MismatchedDevicesException(mismatchedDevices); throw new MismatchedDevicesException(mismatchedDevices);
case 410: case 410:
StaleDevices staleDevices; StaleDevices staleDevices = readResponseJson(response, StaleDevices.class);
try {
staleDevices = JsonUtil.fromJson(readBodyString(responseBody), StaleDevices.class);
} catch (JsonProcessingException e) {
throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
} catch (IOException e) {
throw new PushNetworkException(e);
}
throw new StaleDevicesException(staleDevices); throw new StaleDevicesException(staleDevices);
case 411: case 411:
DeviceLimit deviceLimit; DeviceLimit deviceLimit = readResponseJson(response, DeviceLimit.class);
try {
deviceLimit = JsonUtil.fromJson(readBodyString(responseBody), DeviceLimit.class);
} catch (JsonProcessingException e) {
throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
} catch (IOException e) {
throw new PushNetworkException(e);
}
throw new DeviceLimitExceededException(deviceLimit); throw new DeviceLimitExceededException(deviceLimit);
case 417: case 417:
throw new ExpectationFailedException(); throw new ExpectationFailedException();
case 423: case 423:
RegistrationLockFailure accountLockFailure; RegistrationLockFailure accountLockFailure = readResponseJson(response, RegistrationLockFailure.class);
AuthCredentials credentials = accountLockFailure.backupCredentials;
try { String basicStorageCredentials = credentials != null ? credentials.asBasic() : null;
accountLockFailure = JsonUtil.fromJson(readBodyString(responseBody), RegistrationLockFailure.class);
} catch (JsonProcessingException e) {
Log.w(TAG, e);
throw new NonSuccessfulResponseCodeException("Bad response: " + responseCode + " " + responseMessage);
} catch (IOException e) {
throw new PushNetworkException(e);
}
AuthCredentials credentials = accountLockFailure.backupCredentials;
String basicStorageCredentials = credentials != null ? credentials.asBasic() : null;
throw new LockedException(accountLockFailure.length, throw new LockedException(accountLockFailure.length,
accountLockFailure.timeRemaining, accountLockFailure.timeRemaining,
@ -1797,14 +1752,52 @@ public class PushServiceSocket {
} }
} }
private static String readBodyString(ResponseBody body) throws IOException { /**
if (body != null) { * Converts {@link IOException} on body reading to {@link PushNetworkException}.
*/
private static String readBodyString(ResponseBody body) throws PushNetworkException {
if (body == null) {
throw new PushNetworkException("No body!");
}
try {
return body.string(); return body.string();
} else { } catch (IOException e) {
throw new IOException("No body!"); throw new PushNetworkException(e);
} }
} }
/**
* Converts {@link IOException} on body reading to {@link PushNetworkException}.
* {@link IOException} during json parsing is converted to a {@link NonSuccessfulResponseCodeException}
*/
private static <T> T readBodyJson(ResponseBody body, Class<T> clazz)
throws PushNetworkException, NonSuccessfulResponseCodeException
{
String json = readBodyString(body);
try {
return JsonUtil.fromJson(json, clazz);
} catch (JsonProcessingException e) {
Log.w(TAG, e);
throw new NonSuccessfulResponseCodeException("Unable to parse entity");
} catch (IOException e) {
throw new PushNetworkException(e);
}
}
/**
* Converts {@link IOException} on body reading to {@link PushNetworkException}.
* {@link IOException} during json parsing is converted to a {@link NonSuccessfulResponseCodeException} with response code detail.
*/
private static <T> T readResponseJson(Response response, Class<T> clazz)
throws PushNetworkException, NonSuccessfulResponseCodeException
{
try {
return readBodyJson(response.body(), clazz);
} catch (NonSuccessfulResponseCodeException e) {
throw new NonSuccessfulResponseCodeException("Bad response: " + response.code() + " " + response.message());
}
}
private static class GcmRegistrationId { private static class GcmRegistrationId {