From 841fbfa7ee38d4d9e58c834fce1f5bff7f4e89ba Mon Sep 17 00:00:00 2001 From: Nicholas Date: Mon, 1 May 2023 11:49:24 -0400 Subject: [PATCH] Improve error handling for external failures during registration. Addresses #10711 and #12927. --- .../ExternalServiceFailureException.kt | 6 ++- .../internal/push/PushServiceSocket.java | 49 +++++++++++-------- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/libsignal/service/src/main/java/org/whispersystems/signalservice/api/push/exceptions/ExternalServiceFailureException.kt b/libsignal/service/src/main/java/org/whispersystems/signalservice/api/push/exceptions/ExternalServiceFailureException.kt index 98dceb0200..93c6ddb6f7 100644 --- a/libsignal/service/src/main/java/org/whispersystems/signalservice/api/push/exceptions/ExternalServiceFailureException.kt +++ b/libsignal/service/src/main/java/org/whispersystems/signalservice/api/push/exceptions/ExternalServiceFailureException.kt @@ -6,4 +6,8 @@ package org.whispersystems.signalservice.api.push.exceptions * providerUnavailable - indicates that the provider could not be reached or did not respond to the request to send a verification code in a timely manner * illegalArgument - some part of the request was not understood or accepted by the provider (e.g. the provider did not recognize the phone number as a valid number for the selected transport) */ -class ExternalServiceFailureException(val isPermanent: Boolean, val reason: String) : NonSuccessfulResponseCodeException(502) +class ExternalServiceFailureException(val isPermanent: Boolean, val reason: String) : NonSuccessfulResponseCodeException(502) { + override fun toString(): String { + return "ExternalServiceFailureException: External service failed to send SMS code ${if (isPermanent) "permanently" else "temporarily"} due to $reason" + } +} diff --git a/libsignal/service/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java b/libsignal/service/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java index ff5bfe1d46..0db132a117 100644 --- a/libsignal/service/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java +++ b/libsignal/service/src/main/java/org/whispersystems/signalservice/internal/push/PushServiceSocket.java @@ -49,7 +49,6 @@ import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemo import org.whispersystems.signalservice.api.messages.calls.CallingResponse; import org.whispersystems.signalservice.api.messages.calls.TurnServerInfo; import org.whispersystems.signalservice.api.messages.multidevice.DeviceInfo; -import org.whispersystems.signalservice.api.messages.multidevice.VerifyDeviceResponse; import org.whispersystems.signalservice.api.payments.CurrencyConversions; import org.whispersystems.signalservice.api.profiles.ProfileAndCredential; import org.whispersystems.signalservice.api.profiles.SignalServiceProfile; @@ -2525,26 +2524,34 @@ public class PushServiceSocket { @Override public void handle(int responseCode, ResponseBody body) throws NonSuccessfulResponseCodeException, PushNetworkException { - switch (responseCode) { - case 403: - throw new IncorrectRegistrationRecoveryPasswordException(); - case 404: - throw new NoSuchSessionException(); - case 409: - RegistrationSessionMetadataJson response; - try { - response = JsonUtil.fromJson(body.string(), RegistrationSessionMetadataJson.class); - } catch (IOException e) { - Log.e(TAG, "Unable to read response body.", e); - throw new NonSuccessfulResponseCodeException(409); - } - if (response.pushChallengedRequired()) { - throw new PushChallengeRequiredException(); - } else if (response.captchaRequired()) { - throw new CaptchaRequiredException(); - } else { - throw new HttpConflictException(); - } + if (responseCode == 403) { + throw new IncorrectRegistrationRecoveryPasswordException(); + } else if (responseCode == 404) { + throw new NoSuchSessionException(); + } else if (responseCode == 409) { + RegistrationSessionMetadataJson response; + try { + response = JsonUtil.fromJson(body.string(), RegistrationSessionMetadataJson.class); + } catch (IOException e) { + Log.e(TAG, "Unable to read response body.", e); + throw new NonSuccessfulResponseCodeException(409); + } + if (response.pushChallengedRequired()) { + throw new PushChallengeRequiredException(); + } else if (response.captchaRequired()) { + throw new CaptchaRequiredException(); + } else { + throw new HttpConflictException(); + } + } else if (responseCode == 502) { + VerificationCodeFailureResponseBody response; + try { + response = JsonUtil.fromJson(body.string(), VerificationCodeFailureResponseBody.class); + } catch (IOException e) { + Log.e(TAG, "Unable to read response body.", e); + throw new NonSuccessfulResponseCodeException(502); + } + throw new ExternalServiceFailureException(response.getPermanentFailure(), response.getReason()); } } }