D4770: url: move _wraphttpresponse() from httpeer

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Thu Sep 27 01:10:23 UTC 2018


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This is a generally useful function. Having it on the url module
  will make it more accessible outside of the HTTP peers.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D4770

AFFECTED FILES
  mercurial/httppeer.py
  mercurial/url.py

CHANGE DETAILS

diff --git a/mercurial/url.py b/mercurial/url.py
--- a/mercurial/url.py
+++ b/mercurial/url.py
@@ -595,3 +595,39 @@
         url_ = 'file://' + pycompat.bytesurl(urlreq.pathname2url(path))
         authinfo = None
     return opener(ui, authinfo).open(pycompat.strurl(url_), data)
+
+def wrapresponse(resp):
+    """Wrap a response object with common error handlers.
+
+    This ensures that any I/O from any consumer raises the appropriate
+    error and messaging.
+    """
+    origread = resp.read
+
+    class readerproxy(resp.__class__):
+        def read(self, size=None):
+            try:
+                return origread(size)
+            except httplib.IncompleteRead as e:
+                # e.expected is an integer if length known or None otherwise.
+                if e.expected:
+                    got = len(e.partial)
+                    total = e.expected + got
+                    msg = _('HTTP request error (incomplete response; '
+                            'expected %d bytes got %d)') % (total, got)
+                else:
+                    msg = _('HTTP request error (incomplete response)')
+
+                raise error.PeerTransportError(
+                    msg,
+                    hint=_('this may be an intermittent network failure; '
+                           'if the error persists, consider contacting the '
+                           'network or server operator'))
+            except httplib.HTTPException as e:
+                raise error.PeerTransportError(
+                    _('HTTP request error (%s)') % e,
+                    hint=_('this may be an intermittent network failure; '
+                           'if the error persists, consider contacting the '
+                           'network or server operator'))
+
+    resp.__class__ = readerproxy
diff --git a/mercurial/httppeer.py b/mercurial/httppeer.py
--- a/mercurial/httppeer.py
+++ b/mercurial/httppeer.py
@@ -69,42 +69,6 @@
 
     return result
 
-def _wraphttpresponse(resp):
-    """Wrap an HTTPResponse with common error handlers.
-
-    This ensures that any I/O from any consumer raises the appropriate
-    error and messaging.
-    """
-    origread = resp.read
-
-    class readerproxy(resp.__class__):
-        def read(self, size=None):
-            try:
-                return origread(size)
-            except httplib.IncompleteRead as e:
-                # e.expected is an integer if length known or None otherwise.
-                if e.expected:
-                    got = len(e.partial)
-                    total = e.expected + got
-                    msg = _('HTTP request error (incomplete response; '
-                            'expected %d bytes got %d)') % (total, got)
-                else:
-                    msg = _('HTTP request error (incomplete response)')
-
-                raise error.PeerTransportError(
-                    msg,
-                    hint=_('this may be an intermittent network failure; '
-                           'if the error persists, consider contacting the '
-                           'network or server operator'))
-            except httplib.HTTPException as e:
-                raise error.PeerTransportError(
-                    _('HTTP request error (%s)') % e,
-                    hint=_('this may be an intermittent network failure; '
-                           'if the error persists, consider contacting the '
-                           'network or server operator'))
-
-    resp.__class__ = readerproxy
-
 class _multifile(object):
     def __init__(self, *fileobjs):
         for f in fileobjs:
@@ -325,7 +289,7 @@
                 % (util.timer() - start, code))
 
     # Insert error handlers for common I/O failures.
-    _wraphttpresponse(res)
+    urlmod.wrapresponse(res)
 
     return res
 



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list