Improve error handling for external failures during registration.

Addresses #10711 and #12927.
This commit is contained in:
Nicholas 2023-05-01 11:49:24 -04:00 committed by Alex Hart
parent 89397ae7cc
commit 841fbfa7ee
2 changed files with 33 additions and 22 deletions

View file

@ -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"
}
}

View file

@ -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());
}
}
}