Patchwork D9318: errors: catch urllib errors specifically instead of using safehasattr()

login
register
mail settings
Submitter phabricator
Date Nov. 13, 2020, 7:39 a.m.
Message ID <differential-rev-PHID-DREV-ohl4d53s3cih4a6xdif4-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47593/
State Superseded
Headers show

Comments

phabricator - Nov. 13, 2020, 7:39 a.m.
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 <https://phab.mercurial-scm.org/rHGfbe8834923c57e2222b621c3cc1a78f326ed59d6>
  (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

Patch

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