New UnexpectedOutput exception to catch server errors in localrepo.stream_in
authorThomas Arendsen Hein <thomas@intevation.de>
Fri, 27 Oct 2006 18:17:12 +0200
changeset 3564 eda9e7c9300d
parent 3563 db946221a58a
child 3565 9073d7366776
New UnexpectedOutput exception to catch server errors in localrepo.stream_in If the unexpected is a string, the empty string will be mentioned, and long strings are cut to at most 400 chars.
mercurial/commands.py
mercurial/localrepo.py
mercurial/util.py
--- a/mercurial/commands.py	Fri Oct 27 15:56:34 2006 +0200
+++ b/mercurial/commands.py	Fri Oct 27 18:17:12 2006 +0200
@@ -3522,6 +3522,15 @@
             u.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
         else:
             u.warn(_("abort: %s\n") % inst.strerror)
+    except util.UnexpectedOutput, inst:
+        u.warn(_("abort: %s") % inst[0])
+        if not isinstance(inst[1], basestring):
+            u.warn(" %r\n" % (inst[1],))
+        elif not inst[1]:
+            u.warn(_(" empty string\n"))
+        else:
+            u.warn("\n%r%s\n" %
+                    (inst[1][:400], len(inst[1]) > 400 and '...' or ''))
     except util.Abort, inst:
         u.warn(_("abort: %s\n") % inst)
     except TypeError, inst:
--- a/mercurial/localrepo.py	Fri Oct 27 15:56:34 2006 +0200
+++ b/mercurial/localrepo.py	Fri Oct 27 18:17:12 2006 +0200
@@ -1783,17 +1783,32 @@
 
     def stream_in(self, remote):
         fp = remote.stream_out()
-        resp = int(fp.readline())
+        l = fp.readline()
+        try:
+            resp = int(l)
+        except ValueError:
+            raise util.UnexpectedOutput(
+                _('Unexpected response from remote server:'), l)
         if resp != 0:
             raise util.Abort(_('operation forbidden by server'))
         self.ui.status(_('streaming all changes\n'))
-        total_files, total_bytes = map(int, fp.readline().split(' ', 1))
+        l = fp.readline()
+        try:
+            total_files, total_bytes = map(int, l.split(' ', 1))
+        except ValueError, TypeError:
+            raise util.UnexpectedOutput(
+                _('Unexpected response from remote server:'), l)
         self.ui.status(_('%d files to transfer, %s of data\n') %
                        (total_files, util.bytecount(total_bytes)))
         start = time.time()
         for i in xrange(total_files):
-            name, size = fp.readline().split('\0', 1)
-            size = int(size)
+            l = fp.readline()
+            try:
+                name, size = l.split('\0', 1)
+                size = int(size)
+            except ValueError, TypeError:
+                raise util.UnexpectedOutput(
+                    _('Unexpected response from remote server:'), l)
             self.ui.debug('adding %s (%s)\n' % (name, util.bytecount(size)))
             ofp = self.sopener(name, 'w')
             for chunk in util.filechunkiter(fp, limit=size):
--- a/mercurial/util.py	Fri Oct 27 15:56:34 2006 +0200
+++ b/mercurial/util.py	Fri Oct 27 18:17:12 2006 +0200
@@ -136,6 +136,9 @@
 class Abort(Exception):
     """Raised if a command needs to print an error and exit."""
 
+class UnexpectedOutput(Abort):
+    """Raised to print an error with part of output and exit."""
+
 def always(fn): return True
 def never(fn): return False