Throw TolerableFailureException when deleting a contact returns 404

This commit is contained in:
Torsten Grote
2022-01-07 12:03:21 -03:00
parent df4e6aa207
commit f5cdad9100
3 changed files with 26 additions and 12 deletions

View File

@@ -50,9 +50,12 @@ interface MailboxApi {
/**
* Gets a list of {@link ContactId}s from the mailbox.
* These are the contacts that the mailbox already knows about.
*
* @throws TolerableFailureException if response code is 404
* (contact probably was already deleted).
*/
Collection<ContactId> getContacts(MailboxProperties properties)
throws IOException, ApiException;
throws IOException, ApiException, TolerableFailureException;
@Immutable
@JsonSerialize

View File

@@ -89,11 +89,7 @@ class MailboxApiImpl implements MailboxApi {
public boolean checkStatus(MailboxProperties properties)
throws IOException, ApiException {
if (!properties.isOwner()) throw new IllegalArgumentException();
Request request = getRequestBuilder(properties.getAuthToken())
.url(properties.getOnionAddress() + "/status")
.build();
OkHttpClient client = httpClientProvider.get();
Response response = client.newCall(request).execute();
Response response = sendGetRequest(properties, "/status");
if (response.code() == 401) throw new ApiException();
return response.isSuccessful();
}
@@ -132,13 +128,10 @@ class MailboxApiImpl implements MailboxApi {
@Override
public Collection<ContactId> getContacts(MailboxProperties properties)
throws IOException, ApiException {
throws IOException, ApiException, TolerableFailureException {
if (!properties.isOwner()) throw new IllegalArgumentException();
Request request = getRequestBuilder(properties.getAuthToken())
.url(properties.getOnionAddress() + "/contacts")
.build();
OkHttpClient client = httpClientProvider.get();
Response response = client.newCall(request).execute();
Response response = sendGetRequest(properties, "/contacts");
if (response.code() == 404) throw new TolerableFailureException();
if (response.code() != 200) throw new ApiException();
ResponseBody body = response.body();
@@ -162,6 +155,15 @@ class MailboxApiImpl implements MailboxApi {
}
}
private Response sendGetRequest(MailboxProperties properties, String path)
throws IOException {
Request request = getRequestBuilder(properties.getAuthToken())
.url(properties.getOnionAddress() + path)
.build();
OkHttpClient client = httpClientProvider.get();
return client.newCall(request).execute();
}
private Request.Builder getRequestBuilder(String token) {
return new Request.Builder()
.addHeader("Authorization", "Bearer " + token);