]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/App.java
Close Manager if checkAccountState fails
[signal-cli] / src / main / java / org / asamk / signal / App.java
index 720a58499e9c12accc64ee329781704b3c865749..03bb6fcb2e0b57c55624ee1816ae431597222221 100644 (file)
@@ -32,8 +32,11 @@ import org.freedesktop.dbus.exceptions.DBusExecutionException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.BufferedWriter;
 import java.io.File;
 import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -105,9 +108,10 @@ public class App {
         var outputType = outputTypeInput == null
                 ? command.getSupportedOutputTypes().stream().findFirst().orElse(null)
                 : outputTypeInput;
+        var writer = new BufferedWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()));
         var outputWriter = outputType == null
                 ? null
-                : outputType == OutputType.JSON ? new JsonWriterImpl(System.out) : new PlainTextWriterImpl(System.out);
+                : outputType == OutputType.JSON ? new JsonWriterImpl(writer) : new PlainTextWriterImpl(writer);
 
         if (outputWriter != null && !command.getSupportedOutputTypes().contains(outputType)) {
             throw new UserErrorException("Command doesn't support output type " + outputType);
@@ -311,6 +315,11 @@ public class App {
         try {
             manager.checkAccountState();
         } catch (IOException e) {
+            try {
+                manager.close();
+            } catch (IOException ie) {
+                logger.warn("Failed to close broken account", ie);
+            }
             throw new IOErrorException("Error while checking account " + username + ": " + e.getMessage(), e);
         }
 
@@ -344,11 +353,11 @@ public class App {
             Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter
     ) throws CommandException {
         if (command instanceof LocalCommand localCommand) {
-            try {
-                localCommand.handleCommand(ns, new DbusManagerImpl(ts, dBusConn), outputWriter);
+            try (final var m = new DbusManagerImpl(ts, dBusConn)) {
+                localCommand.handleCommand(ns, m, outputWriter);
             } catch (UnsupportedOperationException e) {
                 throw new UserErrorException("Command is not yet implemented via dbus", e);
-            } catch (DBusExecutionException e) {
+            } catch (IOException | DBusExecutionException e) {
                 throw new UnexpectedErrorException(e.getMessage(), e);
             }
         } else {