]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/jsonrpc/SignalJsonRpcCommandHandler.java
add group info on json message
[signal-cli] / src / main / java / org / asamk / signal / jsonrpc / SignalJsonRpcCommandHandler.java
index 720f22e6ca9ce382c1bdc782503251c51c1f8c68..96456a4edda31b2585d84fd87825e867a36d25e2 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;
@@ -30,11 +32,12 @@ import java.util.function.Function;
 
 public class SignalJsonRpcCommandHandler {
 
-    private final static Logger logger = LoggerFactory.getLogger(SignalJsonRpcDispatcherHandler.class);
+    private static final Logger logger = LoggerFactory.getLogger(SignalJsonRpcDispatcherHandler.class);
 
     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;
@@ -91,7 +94,7 @@ public class SignalJsonRpcCommandHandler {
             if (manager == null) {
                 final var managers = c.getManagers();
                 if (managers.size() == 1) {
-                    manager = managers.get(0);
+                    manager = managers.getFirst();
                 }
             }
             if (manager != null) {
@@ -211,22 +214,32 @@ 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 -> {
+                    logger.error("Command execution failed with unexpected error", e);
+                    throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
+                            e.getMessage() + " (" + e.getClass().getSimpleName() + ")",
+                            getErrorDataNode(objectMapper, result)));
+                }
+            }
         } catch (Throwable e) {
             logger.error("Command execution failed", e);
             throw new JsonRpcException(new JsonRpcResponse.Error(JsonRpcResponse.Error.INTERNAL_ERROR,
-                    e.getMessage(),
+                    e.getMessage() + " (" + e.getClass().getSimpleName() + ")",
                     getErrorDataNode(objectMapper, result)));
         }