Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Unified Diff: gerrit_util.py

Issue 278203002: gerrit_util is aware of auth errors (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/tools/depot_tools
Patch Set: Removed host from GerritAuthenticationError because it should be GerritError, out of scope of this … Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | trychange.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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())
« no previous file with comments | « no previous file | trychange.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698