Repository: borgbackup/borg

Issue:
borg init shows traceback when repository already exists
```
Remote: Borg 1.0.0: exception in RPC call:
Remote: Traceback (most recent call last):
Remote:   File "/usr/lib/python3/dist-packages/borg/remote.py", line 94, in serve
Remote:     res = f(*args)
Remote:   File "/usr/lib/python3/dist-packages/borg/remote.py", line 119, in open
Remote:     self.repository = Repository(path, create, lock_wait=lock_wait, lock=lock)
Remote:   File "/usr/lib/python3/dist-packages/borg/repository.py", line 63, in __init__
Remote:     self.create(self.path)
Remote:   File "/usr/lib/python3/dist-packages/borg/repository.py", line 76, in create
Remote:     raise self.AlreadyExists(path)
Remote: borg.repository.AlreadyExists: /repository
Remote: Platform: Linux computer 4.2.0-34-generic #39~14.04.1-Ubuntu SMP Fri Mar 11 11:38:02 UTC 2016 x86_64 x86_64
Remote: Linux: Ubuntu 14.04 trusty  LibC: glibc 2.9
Remote: Borg: 1.0.0  Python: CPython 3.4.3
Remote: PID: 5778  CWD: /home/username
Remote: sys.argv: ['/usr/bin/borg', 'serve', '--umask=077']
Remote: SSH_ORIGINAL_COMMAND: None
Remote: 
Repository computer:/repository already exists.
```

The error message is at the end, but for some reason the traceback is shown too.



Discussion:

Resolving patch:
diff --git a/borg/remote.py b/borg/remote.py
--- a/borg/remote.py
+++ b/borg/remote.py
@@ -81,6 +81,7 @@ def serve(self):
                 unpacker.feed(data)
                 for unpacked in unpacker:
                     if not (isinstance(unpacked, tuple) and len(unpacked) == 4):
+                        self.repository.close()
                         raise Exception("Unexpected RPC data format.")
                     type, msgid, method, args = unpacked
                     method = method.decode('ascii')
@@ -93,8 +94,12 @@ def serve(self):
                             f = getattr(self.repository, method)
                         res = f(*args)
                     except BaseException as e:
-                        logging.exception('Borg %s: exception in RPC call:', __version__)
-                        logging.error(sysinfo())
+                        # These exceptions are reconstructed on the client end in RemoteRepository.call_many(),
+                        # and will be handled just like locally raised exceptions. Suppress the remote traceback
+                        # for these, except ErrorWithTraceback, which should always display a traceback.
+                        if not isinstance(e, (Repository.DoesNotExist, Repository.AlreadyExists, PathNotAllowed)):
+                            logging.exception('Borg %s: exception in RPC call:', __version__)
+                            logging.error(sysinfo())
                         exc = "Remote Exception (see remote log for the traceback)"
                         os.write(stdout_fd, msgpack.packb((1, msgid, e.__class__.__name__, exc)))
                     else:
@@ -161,7 +166,11 @@ def __init__(self, location, create=False, lock_wait=None, lock=True, args=None)
             raise ConnectionClosedWithHint('Is borg working on the server?') from None
         if version != RPC_PROTOCOL_VERSION:
             raise Exception('Server insisted on using unsupported protocol version %d' % version)
-        self.id = self.call('open', location.path, create, lock_wait, lock)
+        try:
+            self.id = self.call('open', self.location.path, create, lock_wait, lock)
+        except Exception:
+            self.close()
+            raise
 
     def __del__(self):
         self.close()


Test patch: