From patchwork Fri Nov 13 07:39:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: D9318: errors: catch urllib errors specifically instead of using safehasattr() From: phabricator X-Patchwork-Id: 47593 Message-Id: To: Phabricator Cc: mercurial-devel@mercurial-scm.org Date: Fri, 13 Nov 2020 07:39:25 +0000 martinvonz created this revision. Herald added a reviewer: hg-reviewers. Herald added a subscriber: mercurial-patches. REVISION SUMMARY Before this patch, we would catch `IOError` and `OSError` and check if the instance had a `.code` member (indicates `HTTPError`) or a `.reason` member (indicates the more generic `URLError`). It seems to me that can simply catch those exception specifically instead, so that's what this code does. The existing code is from fbe8834923c5 (commands: report http exceptions nicely, 2005-06-17), so I suspect it's just that there was no `urllib2` (where `URLError` lives) back then. REPOSITORY rHG Mercurial BRANCH default REVISION DETAIL https://phab.mercurial-scm.org/D9318 AFFECTED FILES mercurial/scmutil.py CHANGE DETAILS To: martinvonz, #hg-reviewers Cc: mercurial-patches, mercurial-devel diff --git a/mercurial/scmutil.py b/mercurial/scmutil.py --- a/mercurial/scmutil.py +++ b/mercurial/scmutil.py @@ -236,20 +236,20 @@ ui.error(_(b"(did you forget to compile extensions?)\n")) elif m in b"zlib".split(): ui.error(_(b"(is your Python install correct?)\n")) + except util.urlerr.httperror as inst: + ui.error(_(b"abort: %s\n") % stringutil.forcebytestr(inst)) + except util.urlerr.urlerror as inst: + try: # usually it is in the form (errno, strerror) + reason = inst.reason.args[1] + except (AttributeError, IndexError): + # it might be anything, for example a string + reason = inst.reason + if isinstance(reason, pycompat.unicode): + # SSLError of Python 2.7.9 contains a unicode + reason = encoding.unitolocal(reason) + ui.error(_(b"abort: error: %s\n") % stringutil.forcebytestr(reason)) except (IOError, OSError) as inst: - if util.safehasattr(inst, b"code"): # HTTPError - ui.error(_(b"abort: %s\n") % stringutil.forcebytestr(inst)) - elif util.safehasattr(inst, b"reason"): # URLError or SSLError - try: # usually it is in the form (errno, strerror) - reason = inst.reason.args[1] - except (AttributeError, IndexError): - # it might be anything, for example a string - reason = inst.reason - if isinstance(reason, pycompat.unicode): - # SSLError of Python 2.7.9 contains a unicode - reason = encoding.unitolocal(reason) - ui.error(_(b"abort: error: %s\n") % stringutil.forcebytestr(reason)) - elif ( + if ( util.safehasattr(inst, b"args") and inst.args and inst.args[0] == errno.EPIPE