Rename PermanentFailureException to ApiException

This commit is contained in:
Torsten Grote
2022-01-07 10:12:15 -03:00
parent 835e9f6994
commit e52c5ddc8e
3 changed files with 24 additions and 34 deletions

View File

@@ -14,19 +14,19 @@ interface MailboxApi {
*
* @param properties MailboxProperties with the setup token
* @return the owner token
* @throws PermanentFailureException for 401 response.
* @throws ApiException for 401 response.
*/
String setup(MailboxProperties properties)
throws IOException, PermanentFailureException;
throws IOException, ApiException;
/**
* Checks the status of the mailbox.
*
* @return true if the status is OK, false otherwise.
* @throws PermanentFailureException for 401 response.
* @throws ApiException for 401 response.
*/
boolean checkStatus(MailboxProperties properties)
throws IOException, PermanentFailureException;
throws IOException, ApiException;
/**
* Adds a new contact to the mailbox.
@@ -35,7 +35,7 @@ interface MailboxApi {
* (contact was already added).
*/
void addContact(MailboxProperties properties, MailboxContact contact)
throws IOException, PermanentFailureException,
throws IOException, ApiException,
TolerableFailureException;
@Immutable
@@ -55,7 +55,7 @@ interface MailboxApi {
}
@Immutable
class PermanentFailureException extends Exception {
class ApiException extends Exception {
}
/**

View File

@@ -41,7 +41,7 @@ class MailboxApiImpl implements MailboxApi {
@Override
public String setup(MailboxProperties properties)
throws IOException, PermanentFailureException {
throws IOException, ApiException {
if (!properties.isOwner()) throw new IllegalArgumentException();
Request request = getRequestBuilder(properties.getAuthToken())
.url(properties.getOnionAddress() + "/setup")
@@ -50,23 +50,23 @@ class MailboxApiImpl implements MailboxApi {
OkHttpClient client = httpClientProvider.get();
Response response = client.newCall(request).execute();
// TODO consider throwing a special exception for the 401 case
if (response.code() == 401) throw new PermanentFailureException();
if (!response.isSuccessful()) throw new PermanentFailureException();
if (response.code() == 401) throw new ApiException();
if (!response.isSuccessful()) throw new ApiException();
ResponseBody body = response.body();
if (body == null) throw new PermanentFailureException();
if (body == null) throw new ApiException();
try {
JsonNode node = mapper.readTree(body.string());
JsonNode tokenNode = node.get("token");
if (tokenNode == null) {
throw new PermanentFailureException();
throw new ApiException();
}
String ownerToken = tokenNode.textValue();
if (ownerToken == null || !isValidToken(ownerToken)) {
throw new PermanentFailureException();
throw new ApiException();
}
return ownerToken;
} catch (JacksonException e) {
throw new PermanentFailureException();
throw new ApiException();
}
}
@@ -83,20 +83,20 @@ class MailboxApiImpl implements MailboxApi {
@Override
public boolean checkStatus(MailboxProperties properties)
throws IOException, PermanentFailureException {
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();
if (response.code() == 401) throw new PermanentFailureException();
if (response.code() == 401) throw new ApiException();
return response.isSuccessful();
}
@Override
public void addContact(MailboxProperties properties, MailboxContact contact)
throws IOException, PermanentFailureException,
throws IOException, ApiException,
TolerableFailureException {
if (!properties.isOwner()) throw new IllegalArgumentException();
byte[] bodyBytes = mapper.writeValueAsBytes(contact);

View File

@@ -3,8 +3,8 @@ package org.briarproject.bramble.mailbox;
import org.briarproject.bramble.api.WeakSingletonProvider;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.mailbox.MailboxProperties;
import org.briarproject.bramble.mailbox.MailboxApi.ApiException;
import org.briarproject.bramble.mailbox.MailboxApi.MailboxContact;
import org.briarproject.bramble.mailbox.MailboxApi.PermanentFailureException;
import org.briarproject.bramble.mailbox.MailboxApi.TolerableFailureException;
import org.briarproject.bramble.test.BrambleTestCase;
import org.junit.Test;
@@ -85,50 +85,42 @@ public class MailboxApiTest extends BrambleTestCase {
assertToken(request1, token);
// empty body
assertThrows(PermanentFailureException.class,
() -> api.setup(properties));
assertThrows(ApiException.class, () -> api.setup(properties));
RecordedRequest request2 = server.takeRequest();
assertEquals("/setup", request2.getPath());
assertEquals("PUT", request2.getMethod());
assertToken(request2, token);
// invalid response
assertThrows(PermanentFailureException.class,
() -> api.setup(properties));
assertThrows(ApiException.class, () -> api.setup(properties));
RecordedRequest request3 = server.takeRequest();
assertEquals("/setup", request3.getPath());
assertEquals("PUT", request3.getMethod());
assertToken(request3, token);
// 401 response
assertThrows(PermanentFailureException.class,
() -> api.setup(properties2));
assertThrows(ApiException.class, () -> api.setup(properties2));
RecordedRequest request4 = server.takeRequest();
assertEquals("/setup", request4.getPath());
assertEquals("PUT", request4.getMethod());
assertToken(request4, token2);
// 500 response
assertThrows(PermanentFailureException.class,
() -> api.setup(properties));
assertThrows(ApiException.class, () -> api.setup(properties));
RecordedRequest request5 = server.takeRequest();
assertEquals("/setup", request5.getPath());
assertEquals("PUT", request5.getMethod());
assertToken(request5, token);
// invalid json dict token response
assertThrows(PermanentFailureException.class,
() -> api.setup(properties)
);
assertThrows(ApiException.class, () -> api.setup(properties));
RecordedRequest request6 = server.takeRequest();
assertEquals("/setup", request6.getPath());
assertEquals("PUT", request6.getMethod());
assertToken(request6, token);
// invalid non-hex string token response
assertThrows(PermanentFailureException.class,
() -> api.setup(properties)
);
assertThrows(ApiException.class, () -> api.setup(properties));
RecordedRequest request7 = server.takeRequest();
assertEquals("/setup", request7.getPath());
assertEquals("PUT", request7.getMethod());
@@ -163,9 +155,7 @@ public class MailboxApiTest extends BrambleTestCase {
assertEquals("/status", request1.getPath());
assertToken(request1, token);
assertThrows(PermanentFailureException.class, () ->
api.checkStatus(properties2)
);
assertThrows(ApiException.class, () -> api.checkStatus(properties2));
RecordedRequest request2 = server.takeRequest();
assertEquals("/status", request2.getPath());
assertToken(request2, token2);