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);

View File

@@ -286,6 +286,7 @@ public class MailboxApiTest extends BrambleTestCase {
server.enqueue(new MockResponse().setBody(invalidResponse3));
server.enqueue(new MockResponse().setResponseCode(401));
server.enqueue(new MockResponse().setResponseCode(500));
server.enqueue(new MockResponse().setResponseCode(404));
server.start();
String baseUrl = getBaseUrl(server);
MailboxProperties properties =
@@ -349,6 +350,14 @@ public class MailboxApiTest extends BrambleTestCase {
assertEquals("/contacts", request8.getPath());
assertEquals("GET", request8.getMethod());
assertToken(request8, token);
// tolerable 404 not found error
assertThrows(TolerableFailureException.class,
() -> api.getContacts(properties));
RecordedRequest request9 = server.takeRequest();
assertEquals("/contacts", request9.getPath());
assertEquals("GET", request9.getMethod());
assertToken(request9, token);
}
@Test