Merge "Refactor Pegleg exceptions"

This commit is contained in:
Zuul 2018-11-13 16:50:49 +00:00 committed by Gerrit Code Review
commit f6cfb1cf2a
3 changed files with 50 additions and 73 deletions

View File

@ -27,11 +27,6 @@ Base Exceptions
Git Exceptions Git Exceptions
-------------- --------------
.. autoexception:: pegleg.engine.exceptions.BaseGitException
:members:
:show-inheritance:
:undoc-members:
.. autoexception:: pegleg.engine.exceptions.GitConfigException .. autoexception:: pegleg.engine.exceptions.GitConfigException
:members: :members:
:show-inheritance: :show-inheritance:

View File

@ -12,78 +12,60 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import logging
__all__ = ('PeglegBaseException',
'GitException',
'GitAuthException',
'GitProxyException',
'GitSSHException',
'GitConfigException',
'GitInvalidRepoException')
LOG = logging.getLogger(__name__)
class PeglegBaseException(Exception): class PeglegBaseException(Exception):
"""Base class for Pegleg exception and error handling.""" """The base Pegleg exception for everything."""
message = "Base Pegleg exception"
def __init__(self, message=None, **kwargs): def __init__(self, message=None, **kwargs):
self.message = message or self.message self.message = message or self.message
try: # nosec try:
self.message = self.message % kwargs self.message = self.message.format(**kwargs)
except Exception: except KeyError:
pass LOG.warning("Missing kwargs")
super(PeglegBaseException, self).__init__(self.message) super().__init__(self.message)
class BaseGitException(PeglegBaseException): class GitException(PeglegBaseException):
"""Base class for Git exceptions and error handling."""
message = 'An unknown error occurred while accessing a chart source.'
class GitException(BaseGitException):
"""Exception when an error occurs cloning a Git repository.""" """Exception when an error occurs cloning a Git repository."""
message = ('Git exception occurred: [%(location)s] may not be a valid '
def __init__(self, location, details=None): 'git repository. Details: %(details)s')
self._message = ('Git exception occurred: [%s] may not be a valid git '
'repository' % location)
if details:
self._message += '. Details: %s' % details
super(GitException, self).__init__(self._message)
class GitAuthException(BaseGitException): class GitAuthException(PeglegBaseException):
"""Exception that occurs when authentication fails for cloning a repo.""" """Exception that occurs when authentication fails for cloning a repo."""
message = ('Failed to authenticate for repo %(repo_url)s with ssh-key '
def __init__(self, repo_url, ssh_key_path): 'at path %(ssh_key_path)s')
self._repo_url = repo_url
self._ssh_key_path = ssh_key_path
self._message = ('Failed to authenticate for repo %s with ssh-key at '
'path %s' % (self._repo_url, self._ssh_key_path))
super(GitAuthException, self).__init__(self._message)
class GitProxyException(BaseGitException): class GitProxyException(PeglegBaseException):
"""Exception when an error occurs cloning a Git repository """Exception when cloning through proxy."""
through a proxy.""" message = 'Could not resolve proxy [%(location)s]'
def __init__(self, location):
self._location = location
self._message = ('Could not resolve proxy [%s]' % self._location)
super(GitProxyException, self).__init__(self._message)
class GitSSHException(BaseGitException): class GitSSHException(PeglegBaseException):
"""Exception that occurs when an SSH key could not be found.""" """Exception that occurs when an SSH key could not be found."""
message = 'Failed to find specified SSH key: %(ssh_key_path)s'
def __init__(self, ssh_key_path):
self._ssh_key_path = ssh_key_path
self._message = ('Failed to find specified SSH key: %s' %
(self._ssh_key_path))
super(GitSSHException, self).__init__(self._message)
class GitConfigException(BaseGitException): class GitConfigException(PeglegBaseException):
"""Exception that occurs when reading Git repo config fails.""" """Exception that occurs when reading Git repo config fails."""
message = ("Failed to read Git config file for repo path: %(repo_path)s") message = 'Failed to read Git config file for repo path: %(repo_path)s'
class GitInvalidRepoException(BaseGitException): class GitInvalidRepoException(PeglegBaseException):
"""Exception raised when an invalid repository is detected.""" """Exception raised when an invalid repository is detected."""
message = ("The repository path or URL is invalid: %(repo_path)s") message = 'The repository path or URL is invalid: %(repo_path)s'

View File

@ -200,15 +200,16 @@ def _try_git_clone(repo_url,
ref) ref)
if (ssh_cmd and ssh_cmd in e.stderr if (ssh_cmd and ssh_cmd in e.stderr
or 'permission denied' in e.stderr.lower()): or 'permission denied' in e.stderr.lower()):
raise exceptions.GitAuthException(repo_url, auth_key) raise exceptions.GitAuthException(
repo_url=repo_url, ssh_key_path=auth_key)
elif 'could not resolve proxy' in e.stderr.lower(): elif 'could not resolve proxy' in e.stderr.lower():
raise exceptions.GitProxyException(proxy_server) raise exceptions.GitProxyException(location=proxy_server)
else: else:
raise exceptions.GitException(repo_url, details=e) raise exceptions.GitException(location=repo_url, details=e)
except Exception as e: except Exception as e:
msg = 'Encountered unknown Exception during clone of %s' % repo_url LOG.exception('Encountered unknown Exception during clone of %s',
LOG.exception(msg) repo_url)
raise exceptions.GitException(repo_url, details=e) raise exceptions.GitException(location=repo_url, details=e)
_try_git_checkout(repo=repo, repo_url=repo_url, ref=ref) _try_git_checkout(repo=repo, repo_url=repo_url, ref=ref)
@ -241,7 +242,7 @@ def _get_remote_env_vars(auth_key=None):
else: else:
msg = "The auth_key path '%s' was not found" % auth_key msg = "The auth_key path '%s' was not found" % auth_key
LOG.error(msg) LOG.error(msg)
raise exceptions.GitSSHException(auth_key) raise exceptions.GitSSHException(ssh_key_path=auth_key)
return env_vars return env_vars
@ -297,12 +298,11 @@ def _try_git_checkout(repo, repo_url, ref=None, fetch=True):
except git_exc.GitCommandError as e: except git_exc.GitCommandError as e:
LOG.exception('Failed to checkout ref=%s from repo_url=%s.', ref, LOG.exception('Failed to checkout ref=%s from repo_url=%s.', ref,
repo_url) repo_url)
raise exceptions.GitException(repo_url, details=e) raise exceptions.GitException(location=repo_url, details=e)
except Exception as e: except Exception as e:
msg = ('Encountered unknown Exception during checkout of ref=%s for ' LOG.exception(('Encountered unknown Exception during checkout of '
'repo_url=%s' % (ref, repo_url)) 'ref=%s for repo_url=%s'), ref, repo_url)
LOG.exception(msg) raise exceptions.GitException(location=repo_url, details=e)
raise exceptions.GitException(repo_url, details=e)
def _create_or_checkout_local_ref(g, branches, ref): def _create_or_checkout_local_ref(g, branches, ref):
@ -388,7 +388,7 @@ def repo_name(repo_path):
""" """
if not is_repository(normalize_repo_path(repo_path)[0]): if not is_repository(normalize_repo_path(repo_path)[0]):
raise exceptions.GitConfigException(repo_url=repo_path) raise exceptions.GitConfigException(repo_path=repo_path)
# TODO(felipemonteiro): Support this for remote URLs too? # TODO(felipemonteiro): Support this for remote URLs too?
repo = Repo(repo_path, search_parent_directories=True) repo = Repo(repo_path, search_parent_directories=True)
@ -408,9 +408,9 @@ def repo_name(repo_path):
else: else:
return repo_url.split('/')[-1] return repo_url.split('/')[-1]
except Exception: except Exception:
raise exceptions.GitConfigException(repo_url=repo_path) raise exceptions.GitConfigException(repo_path=repo_path)
raise exceptions.GitConfigException(repo_url=repo_path) raise exceptions.GitConfigException(repo_path=repo_path)
def normalize_repo_path(repo_url_or_path): def normalize_repo_path(repo_url_or_path):