Attach information to ContactExistsException.

This commit is contained in:
akwizgran
2019-05-09 17:46:45 +01:00
parent 9ea91cbb3e
commit bcc899eebf
4 changed files with 27 additions and 7 deletions

View File

@@ -1,8 +1,27 @@
package org.briarproject.bramble.api.db; package org.briarproject.bramble.api.db;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId;
/** /**
* Thrown when a duplicate contact is added to the database. This exception may * Thrown when a duplicate contact is added to the database. This exception may
* occur due to concurrent updates and does not indicate a database error. * occur due to concurrent updates and does not indicate a database error.
*/ */
public class ContactExistsException extends DbException { public class ContactExistsException extends DbException {
private final AuthorId local;
private final Author remote;
public ContactExistsException(AuthorId local, Author remote) {
this.local = local;
this.remote = remote;
}
public AuthorId getLocalAuthorId() {
return local;
}
public Author getRemoteAuthor() {
return remote;
}
} }

View File

@@ -229,7 +229,7 @@ class ContactExchangeManagerImpl implements ContactExchangeManager {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
tryToClose(conn); tryToClose(conn);
eventBus.broadcast( eventBus.broadcast(
new ContactExchangeFailedEvent(remoteInfo.author)); new ContactExchangeFailedEvent(e.getRemoteAuthor()));
} catch (DbException e) { } catch (DbException e) {
logException(LOG, WARNING, e); logException(LOG, WARNING, e);
tryToClose(conn); tryToClose(conn);

View File

@@ -234,16 +234,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
@Override @Override
public ContactId addContact(Transaction transaction, Author remote, public ContactId addContact(Transaction transaction, Author remote,
AuthorId local, boolean verified) AuthorId local, boolean verified) throws DbException {
throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException(); if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsIdentity(txn, local)) if (!db.containsIdentity(txn, local))
throw new NoSuchIdentityException(); throw new NoSuchIdentityException();
if (db.containsIdentity(txn, remote.getId())) if (db.containsIdentity(txn, remote.getId()))
throw new ContactExistsException(); throw new ContactExistsException(local, remote);
if (db.containsContact(txn, remote.getId(), local)) if (db.containsContact(txn, remote.getId(), local))
throw new ContactExistsException(); throw new ContactExistsException(local, remote);
ContactId c = db.addContact(txn, remote, local, verified); ContactId c = db.addContact(txn, remote, local, verified);
transaction.attach(new ContactAddedEvent(c)); transaction.attach(new ContactAddedEvent(c));
return c; return c;

View File

@@ -1459,7 +1459,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
true)); true));
fail(); fail();
} catch (ContactExistsException expected) { } catch (ContactExistsException expected) {
// Expected assertEquals(localAuthor.getId(), expected.getLocalAuthorId());
assertEquals(author, expected.getRemoteAuthor());
} }
} }
@@ -1488,7 +1489,8 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
true)); true));
fail(); fail();
} catch (ContactExistsException expected) { } catch (ContactExistsException expected) {
// Expected assertEquals(localAuthor.getId(), expected.getLocalAuthorId());
assertEquals(author, expected.getRemoteAuthor());
} }
} }