]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/jsonrpc/SignalJsonRpcDispatcherHandler.java
Implement more methods for DbusManagerImpl
[signal-cli] / src / main / java / org / asamk / signal / jsonrpc / SignalJsonRpcDispatcherHandler.java
index cb87687e66fc49cc9b214a140c10af51ddaf6db5..8d13ad2875ad1b819fb23ed9b51643cf686c10ad 100644 (file)
@@ -27,6 +27,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
+import java.nio.channels.OverlappingFileLockException;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Objects;
@@ -182,6 +183,9 @@ public class SignalJsonRpcDispatcherHandler {
                 final var registrationManager = c.getNewRegistrationManager(params.get("account").asText());
                 ((ObjectNode) params).remove("account");
                 return registrationManager;
+            } catch (OverlappingFileLockException e) {
+                logger.warn("Account is already in use");
+                return null;
             } catch (IOException | IllegalStateException e) {
                 logger.warn("Failed to load registration manager", e);
                 return null;
@@ -269,22 +273,35 @@ public class SignalJsonRpcDispatcherHandler {
                     e.getMessage(),
                     null));
         } catch (UserErrorException e) {
-            throw new JsonRpcException(new JsonRpcResponse.Error(USER_ERROR, e.getMessage(), null));
+            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(), null));
+            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(), null));
+            throw new JsonRpcException(new JsonRpcResponse.Error(UNTRUSTED_KEY_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,
                     e.getMessage(),
-                    null));
+                    getErrorDataNode(objectMapper, result)));
         }
 
         Object output = result[0] == null ? Map.of() : result[0];
         return objectMapper.valueToTree(output);
     }
 
+    private JsonNode getErrorDataNode(final ObjectMapper objectMapper, final Object[] result) {
+        if (result[0] == null) {
+            return null;
+        }
+        return objectMapper.valueToTree(Map.of("response", result[0]));
+    }
+
     private <T> void parseParamsAndRunCommand(
             final ObjectMapper objectMapper,
             final TreeNode params,