Mercurial > public > mercurial-scm > hg
comparison mercurial/httppeer.py @ 40018:f80db6adabbe
url: move _wraphttpresponse() from httpeer
This is a generally useful function. Having it on the url module
will make it more accessible outside of the HTTP peers.
Differential Revision: https://phab.mercurial-scm.org/D4770
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Wed, 26 Sep 2018 16:07:59 -0700 |
parents | 9c2c77c73f23 |
children | f5a05bb48116 |
comparison
equal
deleted
inserted
replaced
40017:426cb2859013 | 40018:f80db6adabbe |
---|---|
66 for i in pycompat.xrange(0, len(value), valuelen): | 66 for i in pycompat.xrange(0, len(value), valuelen): |
67 n += 1 | 67 n += 1 |
68 result.append((fmt % str(n), pycompat.strurl(value[i:i + valuelen]))) | 68 result.append((fmt % str(n), pycompat.strurl(value[i:i + valuelen]))) |
69 | 69 |
70 return result | 70 return result |
71 | |
72 def _wraphttpresponse(resp): | |
73 """Wrap an HTTPResponse with common error handlers. | |
74 | |
75 This ensures that any I/O from any consumer raises the appropriate | |
76 error and messaging. | |
77 """ | |
78 origread = resp.read | |
79 | |
80 class readerproxy(resp.__class__): | |
81 def read(self, size=None): | |
82 try: | |
83 return origread(size) | |
84 except httplib.IncompleteRead as e: | |
85 # e.expected is an integer if length known or None otherwise. | |
86 if e.expected: | |
87 got = len(e.partial) | |
88 total = e.expected + got | |
89 msg = _('HTTP request error (incomplete response; ' | |
90 'expected %d bytes got %d)') % (total, got) | |
91 else: | |
92 msg = _('HTTP request error (incomplete response)') | |
93 | |
94 raise error.PeerTransportError( | |
95 msg, | |
96 hint=_('this may be an intermittent network failure; ' | |
97 'if the error persists, consider contacting the ' | |
98 'network or server operator')) | |
99 except httplib.HTTPException as e: | |
100 raise error.PeerTransportError( | |
101 _('HTTP request error (%s)') % e, | |
102 hint=_('this may be an intermittent network failure; ' | |
103 'if the error persists, consider contacting the ' | |
104 'network or server operator')) | |
105 | |
106 resp.__class__ = readerproxy | |
107 | 71 |
108 class _multifile(object): | 72 class _multifile(object): |
109 def __init__(self, *fileobjs): | 73 def __init__(self, *fileobjs): |
110 for f in fileobjs: | 74 for f in fileobjs: |
111 if not util.safehasattr(f, 'length'): | 75 if not util.safehasattr(f, 'length'): |
323 code = res.code if res else -1 | 287 code = res.code if res else -1 |
324 dbg(line % ' finished in %.4f seconds (%d)' | 288 dbg(line % ' finished in %.4f seconds (%d)' |
325 % (util.timer() - start, code)) | 289 % (util.timer() - start, code)) |
326 | 290 |
327 # Insert error handlers for common I/O failures. | 291 # Insert error handlers for common I/O failures. |
328 _wraphttpresponse(res) | 292 urlmod.wrapresponse(res) |
329 | 293 |
330 return res | 294 return res |
331 | 295 |
332 class RedirectedRepoError(error.RepoError): | 296 class RedirectedRepoError(error.RepoError): |
333 def __init__(self, msg, respurl): | 297 def __init__(self, msg, respurl): |