Improve exception handling

This commit is contained in:
AsamK 2016-07-08 11:31:41 +02:00
parent 74fb7d9377
commit c0a0f89896
3 changed files with 53 additions and 52 deletions

View file

@ -12,13 +12,13 @@ import java.io.IOException;
import java.util.List; import java.util.List;
public interface Signal extends DBusInterface { public interface Signal extends DBusInterface {
void sendMessage(String message, List<String> attachments, String recipient) throws EncapsulatedExceptions, AttachmentInvalidException, IOException, UntrustedIdentityException; void sendMessage(String message, List<String> attachments, String recipient) throws EncapsulatedExceptions, AttachmentInvalidException, IOException;
void sendMessage(String message, List<String> attachments, List<String> recipients) throws EncapsulatedExceptions, AttachmentInvalidException, IOException, UntrustedIdentityException; void sendMessage(String message, List<String> attachments, List<String> recipients) throws EncapsulatedExceptions, AttachmentInvalidException, IOException;
void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions, UntrustedIdentityException; void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions;
void sendGroupMessage(String message, List<String> attachments, byte[] groupId) throws EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, IOException, UntrustedIdentityException; void sendGroupMessage(String message, List<String> attachments, byte[] groupId) throws EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, IOException;
class MessageReceived extends DBusSignal { class MessageReceived extends DBusSignal {
private long timestamp; private long timestamp;

View file

@ -265,8 +265,6 @@ public class Main {
handleAssertionError(e); handleAssertionError(e);
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
handleDBusExecutionException(e); handleDBusExecutionException(e);
} catch (UntrustedIdentityException e) {
e.printStackTrace();
} }
} else { } else {
String messageText = ns.getString("message"); String messageText = ns.getString("message");
@ -305,8 +303,6 @@ public class Main {
System.exit(1); System.exit(1);
} catch (DBusExecutionException e) { } catch (DBusExecutionException e) {
handleDBusExecutionException(e); handleDBusExecutionException(e);
} catch (UntrustedIdentityException e) {
e.printStackTrace();
} }
} }
@ -385,8 +381,6 @@ public class Main {
handleAssertionError(e); handleAssertionError(e);
} catch (GroupNotFoundException e) { } catch (GroupNotFoundException e) {
handleGroupNotFoundException(e); handleGroupNotFoundException(e);
} catch (UntrustedIdentityException e) {
e.printStackTrace();
} }
break; break;
@ -419,8 +413,6 @@ public class Main {
handleGroupNotFoundException(e); handleGroupNotFoundException(e);
} catch (EncapsulatedExceptions e) { } catch (EncapsulatedExceptions e) {
handleEncapsulatedExceptions(e); handleEncapsulatedExceptions(e);
} catch (UntrustedIdentityException e) {
e.printStackTrace();
} }
break; break;

View file

@ -47,8 +47,7 @@ import org.whispersystems.signalservice.api.messages.*;
import org.whispersystems.signalservice.api.messages.multidevice.*; import org.whispersystems.signalservice.api.messages.multidevice.*;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.TrustStore; import org.whispersystems.signalservice.api.push.TrustStore;
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException; import org.whispersystems.signalservice.api.push.exceptions.*;
import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter; import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
import org.whispersystems.signalservice.internal.push.SignalServiceProtos; import org.whispersystems.signalservice.internal.push.SignalServiceProtos;
@ -489,7 +488,7 @@ class Manager implements Signal {
@Override @Override
public void sendGroupMessage(String messageText, List<String> attachments, public void sendGroupMessage(String messageText, List<String> attachments,
byte[] groupId) byte[] groupId)
throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, UntrustedIdentityException { throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText); final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
if (attachments != null) { if (attachments != null) {
messageBuilder.withAttachments(getSignalServiceAttachments(attachments)); messageBuilder.withAttachments(getSignalServiceAttachments(attachments));
@ -513,7 +512,7 @@ class Manager implements Signal {
sendMessage(message, membersSend); sendMessage(message, membersSend);
} }
public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions, UntrustedIdentityException { public void sendQuitGroupMessage(byte[] groupId) throws GroupNotFoundException, IOException, EncapsulatedExceptions {
SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT) SignalServiceGroup group = SignalServiceGroup.newBuilder(SignalServiceGroup.Type.QUIT)
.withId(groupId) .withId(groupId)
.build(); .build();
@ -532,7 +531,7 @@ class Manager implements Signal {
sendMessage(message, g.members); sendMessage(message, g.members);
} }
public byte[] sendUpdateGroupMessage(byte[] groupId, String name, Collection<String> members, String avatarFile) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException, UntrustedIdentityException { public byte[] sendUpdateGroupMessage(byte[] groupId, String name, Collection<String> members, String avatarFile) throws IOException, EncapsulatedExceptions, GroupNotFoundException, AttachmentInvalidException {
GroupInfo g; GroupInfo g;
if (groupId == null) { if (groupId == null) {
// Create new group // Create new group
@ -594,7 +593,7 @@ class Manager implements Signal {
@Override @Override
public void sendMessage(String message, List<String> attachments, String recipient) public void sendMessage(String message, List<String> attachments, String recipient)
throws EncapsulatedExceptions, AttachmentInvalidException, IOException, UntrustedIdentityException { throws EncapsulatedExceptions, AttachmentInvalidException, IOException {
List<String> recipients = new ArrayList<>(1); List<String> recipients = new ArrayList<>(1);
recipients.add(recipient); recipients.add(recipient);
sendMessage(message, attachments, recipients); sendMessage(message, attachments, recipients);
@ -603,7 +602,7 @@ class Manager implements Signal {
@Override @Override
public void sendMessage(String messageText, List<String> attachments, public void sendMessage(String messageText, List<String> attachments,
List<String> recipients) List<String> recipients)
throws IOException, EncapsulatedExceptions, AttachmentInvalidException, UntrustedIdentityException { throws IOException, EncapsulatedExceptions, AttachmentInvalidException {
final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText); final SignalServiceDataMessage.Builder messageBuilder = SignalServiceDataMessage.newBuilder().withBody(messageText);
if (attachments != null) { if (attachments != null) {
messageBuilder.withAttachments(getSignalServiceAttachments(attachments)); messageBuilder.withAttachments(getSignalServiceAttachments(attachments));
@ -614,7 +613,7 @@ class Manager implements Signal {
} }
@Override @Override
public void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions, UntrustedIdentityException { public void sendEndSessionMessage(List<String> recipients) throws IOException, EncapsulatedExceptions {
SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder() SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
.asEndSessionMessage() .asEndSessionMessage()
.build(); .build();
@ -627,8 +626,6 @@ class Manager implements Signal {
SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r)); SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
try { try {
sendMessage(message); sendMessage(message);
} catch (EncapsulatedExceptions encapsulatedExceptions) {
encapsulatedExceptions.printStackTrace();
} catch (UntrustedIdentityException e) { } catch (UntrustedIdentityException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -639,65 +636,74 @@ class Manager implements Signal {
SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r)); SignalServiceSyncMessage message = SignalServiceSyncMessage.forRequest(new RequestMessage(r));
try { try {
sendMessage(message); sendMessage(message);
} catch (EncapsulatedExceptions encapsulatedExceptions) {
encapsulatedExceptions.printStackTrace();
} catch (UntrustedIdentityException e) { } catch (UntrustedIdentityException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
private void sendMessage(SignalServiceSyncMessage message) private void sendMessage(SignalServiceSyncMessage message)
throws IOException, EncapsulatedExceptions, UntrustedIdentityException { throws IOException, UntrustedIdentityException {
SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password, SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password,
deviceId, signalProtocolStore, USER_AGENT, Optional.<SignalServiceMessageSender.EventListener>absent()); deviceId, signalProtocolStore, USER_AGENT, Optional.<SignalServiceMessageSender.EventListener>absent());
messageSender.sendMessage(message); messageSender.sendMessage(message);
} }
private void sendMessage(SignalServiceDataMessage message, Collection<String> recipients) private void sendMessage(SignalServiceDataMessage message, Collection<String> recipients)
throws IOException, EncapsulatedExceptions, UntrustedIdentityException { throws EncapsulatedExceptions, IOException {
Set<SignalServiceAddress> recipientsTS = new HashSet<>(recipients.size());
for (String recipient : recipients) {
try {
recipientsTS.add(getPushAddress(recipient));
} catch (InvalidNumberException e) {
System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
System.err.println("Aborting sending.");
save();
return;
}
}
try { try {
SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password, SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, username, password,
deviceId, signalProtocolStore, USER_AGENT, Optional.<SignalServiceMessageSender.EventListener>absent()); deviceId, signalProtocolStore, USER_AGENT, Optional.<SignalServiceMessageSender.EventListener>absent());
Set<SignalServiceAddress> recipientsTS = new HashSet<>(recipients.size());
for (String recipient : recipients) {
try {
recipientsTS.add(getPushAddress(recipient));
} catch (InvalidNumberException e) {
System.err.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
System.err.println("Aborting sending.");
save();
return;
}
}
if (message.getGroupInfo().isPresent()) { if (message.getGroupInfo().isPresent()) {
messageSender.sendMessage(new ArrayList<>(recipientsTS), message); messageSender.sendMessage(new ArrayList<>(recipientsTS), message);
} else { } else {
// Send to all individually, so sync messages are sent correctly // Send to all individually, so sync messages are sent correctly
List<UntrustedIdentityException> untrustedIdentities = new LinkedList<>();
List<UnregisteredUserException> unregisteredUsers = new LinkedList<>();
List<NetworkFailureException> networkExceptions = new LinkedList<>();
for (SignalServiceAddress address : recipientsTS) { for (SignalServiceAddress address : recipientsTS) {
messageSender.sendMessage(address, message); try {
messageSender.sendMessage(address, message);
} catch (UntrustedIdentityException e) {
untrustedIdentities.add(e);
} catch (UnregisteredUserException e) {
unregisteredUsers.add(e);
} catch (PushNetworkException e) {
networkExceptions.add(new NetworkFailureException(address.getNumber(), e));
}
}
if (!untrustedIdentities.isEmpty() || !unregisteredUsers.isEmpty() || !networkExceptions.isEmpty()) {
throw new EncapsulatedExceptions(untrustedIdentities, unregisteredUsers, networkExceptions);
} }
} }
} finally {
if (message.isEndSession()) { if (message.isEndSession()) {
for (SignalServiceAddress recipient : recipientsTS) { for (SignalServiceAddress recipient : recipientsTS) {
handleEndSession(recipient.getNumber()); handleEndSession(recipient.getNumber());
} }
} }
} finally {
save(); save();
} }
} }
private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) { private SignalServiceContent decryptMessage(SignalServiceEnvelope envelope) throws NoSessionException, LegacyMessageException, InvalidVersionException, InvalidMessageException, DuplicateMessageException, InvalidKeyException, InvalidKeyIdException, org.whispersystems.libsignal.UntrustedIdentityException {
SignalServiceCipher cipher = new SignalServiceCipher(new SignalServiceAddress(username), signalProtocolStore); SignalServiceCipher cipher = new SignalServiceCipher(new SignalServiceAddress(username), signalProtocolStore);
try { try {
return cipher.decrypt(envelope); return cipher.decrypt(envelope);
} catch (Exception e) { } catch (Exception e) {
// TODO handle all exceptions throw e;
e.printStackTrace();
return null;
} }
} }
@ -781,7 +787,14 @@ class Manager implements Signal {
try { try {
envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS); envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS);
if (!envelope.isReceipt()) { if (!envelope.isReceipt()) {
content = decryptMessage(envelope); Exception exception;
try {
content = decryptMessage(envelope);
} catch (Exception e) {
exception = e;
// TODO pass exception to handler instead
e.printStackTrace();
}
if (content != null) { if (content != null) {
if (content.getDataMessage().isPresent()) { if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get(); SignalServiceDataMessage message = content.getDataMessage().get();
@ -798,8 +811,6 @@ class Manager implements Signal {
if (rm.isContactsRequest()) { if (rm.isContactsRequest()) {
try { try {
sendContacts(); sendContacts();
} catch (EncapsulatedExceptions encapsulatedExceptions) {
encapsulatedExceptions.printStackTrace();
} catch (UntrustedIdentityException e) { } catch (UntrustedIdentityException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -807,8 +818,6 @@ class Manager implements Signal {
if (rm.isGroupsRequest()) { if (rm.isGroupsRequest()) {
try { try {
sendGroups(); sendGroups();
} catch (EncapsulatedExceptions encapsulatedExceptions) {
encapsulatedExceptions.printStackTrace();
} catch (UntrustedIdentityException e) { } catch (UntrustedIdentityException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -1007,7 +1016,7 @@ class Manager implements Signal {
return false; return false;
} }
private void sendGroups() throws IOException, EncapsulatedExceptions, UntrustedIdentityException { private void sendGroups() throws IOException, UntrustedIdentityException {
File groupsFile = File.createTempFile("multidevice-group-update", ".tmp"); File groupsFile = File.createTempFile("multidevice-group-update", ".tmp");
try { try {
@ -1037,7 +1046,7 @@ class Manager implements Signal {
} }
} }
private void sendContacts() throws IOException, EncapsulatedExceptions, UntrustedIdentityException { private void sendContacts() throws IOException, UntrustedIdentityException {
File contactsFile = File.createTempFile("multidevice-contact-update", ".tmp"); File contactsFile = File.createTempFile("multidevice-contact-update", ".tmp");
try { try {