auth: Print specific instructions for ReAuth, other errors

Bug: 442666611
Change-Id: Icfc9c223937b9035f0364630455a50492e282128
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/6912692
Reviewed-by: Jiewei Qian <qjw@chromium.org>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Commit-Queue: Allen Li <ayatane@chromium.org>
This commit is contained in:
Allen Li
2025-09-04 14:15:17 -07:00
committed by LUCI CQ
parent 20ce2373dd
commit 6d9991fc7b

41
auth.py
View File

@@ -75,7 +75,36 @@ class GitLoginRequiredError(Exception):
@property
def login_command(self) -> str:
return 'git-credential-luci login'
return 'git credential-luci login'
class GitReAuthRequiredError(Exception):
"""Interaction with the user is required to ReAuth.
This is for git-credential-luci, not luci-auth.
"""
def __init__(self):
msg = ('You have not done ReAuth. Please run and try again:\n'
' %s' % self.reauth_command)
super(GitReAuthRequiredError, self).__init__(msg)
@property
def reauth_command(self) -> str:
return 'git credential-luci reauth'
class GitUnknownError(Exception):
"""Unknown error from git-credential-luci."""
def __init__(self):
msg = ('Unknown error from git-credential-luci. Try logging in? Run:\n'
' %s' % self.login_command)
super(GitLoginRequiredError, self).__init__(msg)
@property
def login_command(self) -> str:
return 'git credential-luci login'
def has_luci_context_local_auth():
@@ -232,15 +261,17 @@ class GerritAuthenticator(object):
try:
access_token = self._get_luci_auth_token()
if not access_token:
logging.debug('Failed to create access token')
raise GitLoginRequiredError()
raise GitUnknownError()
return access_token
except subprocess2.CalledProcessError as e:
# subprocess2.CalledProcessError.__str__ nicely formats
# stdout/stderr.
logging.error('git-credential-luci failed: %s', e)
logging.debug('Failed to create access token')
raise GitLoginRequiredError()
if e.returncode == 2:
raise GitLoginRequiredError()
if e.returncode == 3:
raise GitReAuthRequiredError()
raise GitUnknownError()
def _get_luci_auth_token(self) -> Optional[str]:
logging.debug('Running git-credential-luci')