Chromium Code Reviews| Index: gerrit_util.py |
| diff --git a/gerrit_util.py b/gerrit_util.py |
| index c8d838c7eaced6565fbb08ec12991cac2e37ba6d..85c5c7ecca4eea04099d939b546c27f7f41de68f 100755 |
| --- a/gerrit_util.py |
| +++ b/gerrit_util.py |
| @@ -14,6 +14,7 @@ import json |
| import logging |
| import netrc |
| import os |
| +import re |
| import time |
| import urllib |
| from cStringIO import StringIO |
| @@ -38,6 +39,10 @@ class GerritError(Exception): |
| self.message = '(%d) %s' % (self.http_status, self.message) |
| +class GerritAuthenticationError(GerritError): |
| + """Exception class for authentication errors during Gerrit communication.""" |
| + |
| + |
| def _QueryString(param_dict, first_param=None): |
| """Encodes query parameters in the key:val[+key:val...] format specified here: |
| @@ -140,6 +145,16 @@ def ReadHttpResponse(conn, expect_status=200, ignore_404=True): |
| if ignore_404 and response.status == 404: |
| return StringIO() |
| if response.status != expect_status: |
| + # Check if this is an authentication issue. |
|
szager1
2014/05/12 08:50:35
Check this in the retry loop. No sense in trying
nodir
2014/05/12 18:11:21
Indeed. Done.
|
| + www_authenticate = response.getheader('www-authenticate') |
| + if (response.status in (httplib.UNAUTHORIZED, httplib.FOUND) and |
| + www_authenticate): |
| + auth_match = re.search('realm="([^"]+)"', www_authenticate, re.I) |
| + host = auth_match.group(1) if auth_match else req_host |
| + reason = ('Authentication failed. Please make sure your .netrc file ' |
| + 'has credentials for %s' % host) |
| + raise GerritAuthenticationError(response.status, reason) |
| + |
| reason = '%s: %s' % (response.reason, response.read()) |
| raise GerritError(response.status, reason) |
| return StringIO(response.read()) |