Add CommandException to abstract cli return codes for errors

This commit is contained in:
AsamK 2021-02-22 20:43:08 +01:00
parent c6395b9f35
commit 221d937eec
47 changed files with 538 additions and 572 deletions

View file

@ -4,7 +4,11 @@ import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.PlainTextWriterImpl;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupId;
import org.asamk.signal.manager.groups.GroupIdFormatException;
import org.asamk.signal.manager.groups.GroupNotFoundException;
import org.asamk.signal.manager.groups.NotAGroupMemberException;
@ -13,10 +17,6 @@ import org.asamk.signal.util.Util;
import java.io.IOException;
import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
import static org.asamk.signal.util.ErrorUtils.handleGroupIdFormatException;
import static org.asamk.signal.util.ErrorUtils.handleGroupNotFoundException;
import static org.asamk.signal.util.ErrorUtils.handleIOException;
import static org.asamk.signal.util.ErrorUtils.handleNotAGroupMemberException;
import static org.asamk.signal.util.ErrorUtils.handleTimestampAndSendMessageResults;
public class QuitGroupCommand implements LocalCommand {
@ -27,28 +27,28 @@ public class QuitGroupCommand implements LocalCommand {
}
@Override
public int handleCommand(final Namespace ns, final Manager m) {
try {
final var writer = new PlainTextWriterImpl(System.out);
public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
final var writer = new PlainTextWriterImpl(System.out);
final var groupId = Util.decodeGroupId(ns.getString("group"));
final GroupId groupId;
try {
groupId = Util.decodeGroupId(ns.getString("group"));
} catch (GroupIdFormatException e) {
throw new UserErrorException("Invalid group id:" + e.getMessage());
}
try {
final var results = m.sendQuitGroupMessage(groupId);
return handleTimestampAndSendMessageResults(writer, results.first(), results.second());
handleTimestampAndSendMessageResults(writer, results.first(), results.second());
} catch (IOException e) {
handleIOException(e);
return 3;
throw new IOErrorException("Failed to send message: " + e.getMessage());
} catch (AssertionError e) {
handleAssertionError(e);
return 1;
throw e;
} catch (GroupNotFoundException e) {
handleGroupNotFoundException(e);
return 1;
throw new UserErrorException("Failed to send to group: " + e.getMessage());
} catch (NotAGroupMemberException e) {
handleNotAGroupMemberException(e);
return 1;
} catch (GroupIdFormatException e) {
handleGroupIdFormatException(e);
return 1;
throw new UserErrorException("Failed to send to group: " + e.getMessage());
}
}
}