changeset 24796:61ff209fc01d

bundle2: refactor error bundle creation for the wireprotocol We want to add output information to the error bundle. Before doing this, we rework the code to have a single bundler creation and return statement. This will make the update with the output simpler as only one place will have to be touched.
author Pierre-Yves David <pierre-yves.david@fb.com>
date Thu, 16 Apr 2015 03:56:50 -0400
parents f9aa4cb8f2dd
children 0c4d5e01b31f
files mercurial/wireproto.py
diffstat 1 files changed, 26 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/wireproto.py	Thu Apr 16 03:17:01 2015 -0400
+++ b/mercurial/wireproto.py	Thu Apr 16 03:56:50 2015 -0400
@@ -841,35 +841,38 @@
         finally:
             fp.close()
             os.unlink(tempname)
-    except error.BundleValueError, exc:
-            bundler = bundle2.bundle20(repo.ui)
+
+    except (error.BundleValueError, util.Abort, error.PushRaced), exc:
+        # handle non-bundle2 case first
+        if not getattr(exc, 'duringunbundle2', False):
+            try:
+                raise
+            except util.Abort:
+                # The old code we moved used sys.stderr directly.
+                # We did not change it to minimise code change.
+                # This need to be moved to something proper.
+                # Feel free to do it.
+                sys.stderr.write("abort: %s\n" % exc)
+                return pushres(0)
+            except error.PushRaced:
+                return pusherr(str(exc))
+
+        bundler = bundle2.bundle20(repo.ui)
+        try:
+            raise
+        except error.BundleValueError, exc:
             errpart = bundler.newpart('error:unsupportedcontent')
             if exc.parttype is not None:
                 errpart.addparam('parttype', exc.parttype)
             if exc.params:
                 errpart.addparam('params', '\0'.join(exc.params))
-            return streamres(bundler.getchunks())
-    except util.Abort, inst:
-        # The old code we moved used sys.stderr directly.
-        # We did not change it to minimise code change.
-        # This need to be moved to something proper.
-        # Feel free to do it.
-        if getattr(inst, 'duringunbundle2', False):
-            bundler = bundle2.bundle20(repo.ui)
-            manargs = [('message', str(inst))]
+        except util.Abort, exc:
+            manargs = [('message', str(exc))]
             advargs = []
-            if inst.hint is not None:
-                advargs.append(('hint', inst.hint))
+            if exc.hint is not None:
+                advargs.append(('hint', exc.hint))
             bundler.addpart(bundle2.bundlepart('error:abort',
                                                manargs, advargs))
-            return streamres(bundler.getchunks())
-        else:
-            sys.stderr.write("abort: %s\n" % inst)
-            return pushres(0)
-    except error.PushRaced, exc:
-        if getattr(exc, 'duringunbundle2', False):
-            bundler = bundle2.bundle20(repo.ui)
+        except error.PushRaced, exc:
             bundler.newpart('error:pushraced', [('message', str(exc))])
-            return streamres(bundler.getchunks())
-        else:
-            return pusherr(str(exc))
+        return streamres(bundler.getchunks())