Skip to content

Commit

Permalink
Handle rate limit error in JSON-RPC mode
Browse files Browse the repository at this point in the history
  • Loading branch information
AsamK committed Nov 5, 2023
1 parent 8d423ad commit 699b21f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/asamk/signal/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private static int getStatusForError(final CommandException e) {
case IOErrorException ioErrorException -> 3;
case UntrustedKeyErrorException untrustedKeyErrorException -> 4;
case RateLimitErrorException rateLimitErrorException -> 5;
case null, default -> 2;
case null -> 2;
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.asamk.signal.commands.exceptions;

public class CommandException extends Exception {
public sealed abstract class CommandException extends Exception permits IOErrorException, RateLimitErrorException, UnexpectedErrorException, UntrustedKeyErrorException, UserErrorException {

public CommandException(final String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.asamk.signal.commands.JsonRpcSingleCommand;
import org.asamk.signal.commands.exceptions.CommandException;
import org.asamk.signal.commands.exceptions.IOErrorException;
import org.asamk.signal.commands.exceptions.RateLimitErrorException;
import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
import org.asamk.signal.commands.exceptions.UserErrorException;
import org.asamk.signal.manager.Manager;
Expand All @@ -35,6 +37,7 @@ public class SignalJsonRpcCommandHandler {
private static final int USER_ERROR = -1;
private static final int IO_ERROR = -3;
private static final int UNTRUSTED_KEY_ERROR = -4;
private static final int RATELIMIT_ERROR = -5;

private final Manager m;
private final MultiAccountManager c;
Expand Down Expand Up @@ -211,18 +214,26 @@ private JsonNode runCommand(
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INVALID_REQUEST,
e.getMessage(),
null));
} catch (UserErrorException e) {
throw new JsonRpcException(new JsonRpcResponse.Error(USER_ERROR,
e.getMessage(),
getErrorDataNode(objectMapper, result)));
} catch (IOErrorException e) {
throw new JsonRpcException(new JsonRpcResponse.Error(IO_ERROR,
e.getMessage(),
getErrorDataNode(objectMapper, result)));
} catch (UntrustedKeyErrorException e) {
throw new JsonRpcException(new JsonRpcResponse.Error(UNTRUSTED_KEY_ERROR,
e.getMessage(),
getErrorDataNode(objectMapper, result)));
} catch (CommandException ce) {
switch (ce) {
case UserErrorException e -> throw new JsonRpcException(new JsonRpcResponse.Error(USER_ERROR,
e.getMessage(),
getErrorDataNode(objectMapper, result)));
case IOErrorException e -> throw new JsonRpcException(new JsonRpcResponse.Error(IO_ERROR,
e.getMessage(),
getErrorDataNode(objectMapper, result)));
case UntrustedKeyErrorException e -> throw new JsonRpcException(new JsonRpcResponse.Error(
UNTRUSTED_KEY_ERROR,
e.getMessage(),
getErrorDataNode(objectMapper, result)));
case RateLimitErrorException e -> throw new JsonRpcException(new JsonRpcResponse.Error(RATELIMIT_ERROR,
e.getMessage(),
getErrorDataNode(objectMapper, result)));
case UnexpectedErrorException e ->
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
e.getMessage(),
getErrorDataNode(objectMapper, result)));
}
} catch (Throwable e) {
logger.error("Command execution failed", e);
throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
Expand Down

0 comments on commit 699b21f

Please sign in to comment.