]> nmode's Git Repositories - signal-cli/commitdiff
Handle rate limit error in JSON-RPC mode
authorAsamK <asamk@gmx.de>
Sun, 5 Nov 2023 15:20:33 +0000 (16:20 +0100)
committerAsamK <asamk@gmx.de>
Sun, 5 Nov 2023 15:20:33 +0000 (16:20 +0100)
src/main/java/org/asamk/signal/Main.java
src/main/java/org/asamk/signal/commands/exceptions/CommandException.java
src/main/java/org/asamk/signal/jsonrpc/SignalJsonRpcCommandHandler.java

index 00d9d7a9fbe2944becb05037d7922b81d96a2494..2b805b685e9939f1bfd9d5ac1435647b6a382ac9 100644 (file)
@@ -115,7 +115,7 @@ public class Main {
             case IOErrorException ioErrorException -> 3;
             case UntrustedKeyErrorException untrustedKeyErrorException -> 4;
             case RateLimitErrorException rateLimitErrorException -> 5;
-            case null, default -> 2;
+            case null -> 2;
         };
     }
 }
index bae7af43aa1d8c2b55fa4a3b92fb7ce4bdf38a2d..739aee9199230731433600279cb9081938da79a7 100644 (file)
@@ -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);
index 720f22e6ca9ce382c1bdc782503251c51c1f8c68..6255672558f26d8b46bb9092a1fa5b0bf882662b 100644 (file)
@@ -14,6 +14,8 @@ import org.asamk.signal.commands.JsonRpcRegistrationCommand;
 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;
@@ -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;
@@ -211,18 +214,26 @@ public class SignalJsonRpcCommandHandler {
             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,