Refactor Pegleg exceptions
This patch set cleans up the current implementation of pegleg. As all the git exceptions inconsistently inits or override the message. This also cleans up a handful of incorrect kwarg keywords, and improve handling of the exception messages. Change-Id: I438eb032728c71cbf972c2120a76d06106cb1580 Signed-off-by: Tin Lam <tin@irrational.io>
This commit is contained in:
parent
1f59d7e1e0
commit
ae81ade94d
@ -27,11 +27,6 @@ Base Exceptions
|
||||
Git Exceptions
|
||||
--------------
|
||||
|
||||
.. autoexception:: pegleg.engine.exceptions.BaseGitException
|
||||
:members:
|
||||
:show-inheritance:
|
||||
:undoc-members:
|
||||
|
||||
.. autoexception:: pegleg.engine.exceptions.GitConfigException
|
||||
:members:
|
||||
:show-inheritance:
|
||||
|
@ -12,78 +12,60 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import logging
|
||||
|
||||
__all__ = ('PeglegBaseException',
|
||||
'GitException',
|
||||
'GitAuthException',
|
||||
'GitProxyException',
|
||||
'GitSSHException',
|
||||
'GitConfigException',
|
||||
'GitInvalidRepoException')
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
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):
|
||||
self.message = message or self.message
|
||||
try: # nosec
|
||||
self.message = self.message % kwargs
|
||||
except Exception:
|
||||
pass
|
||||
super(PeglegBaseException, self).__init__(self.message)
|
||||
try:
|
||||
self.message = self.message.format(**kwargs)
|
||||
except KeyError:
|
||||
LOG.warning("Missing kwargs")
|
||||
super().__init__(self.message)
|
||||
|
||||
|
||||
class BaseGitException(PeglegBaseException):
|
||||
"""Base class for Git exceptions and error handling."""
|
||||
|
||||
message = 'An unknown error occurred while accessing a chart source.'
|
||||
|
||||
|
||||
class GitException(BaseGitException):
|
||||
class GitException(PeglegBaseException):
|
||||
"""Exception when an error occurs cloning a Git repository."""
|
||||
|
||||
def __init__(self, location, details=None):
|
||||
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)
|
||||
message = ('Git exception occurred: [%(location)s] may not be a valid '
|
||||
'git repository. Details: %(details)s')
|
||||
|
||||
|
||||
class GitAuthException(BaseGitException):
|
||||
class GitAuthException(PeglegBaseException):
|
||||
"""Exception that occurs when authentication fails for cloning a repo."""
|
||||
|
||||
def __init__(self, repo_url, ssh_key_path):
|
||||
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)
|
||||
message = ('Failed to authenticate for repo %(repo_url)s with ssh-key '
|
||||
'at path %(ssh_key_path)s')
|
||||
|
||||
|
||||
class GitProxyException(BaseGitException):
|
||||
"""Exception when an error occurs cloning a Git repository
|
||||
through a proxy."""
|
||||
|
||||
def __init__(self, location):
|
||||
self._location = location
|
||||
self._message = ('Could not resolve proxy [%s]' % self._location)
|
||||
|
||||
super(GitProxyException, self).__init__(self._message)
|
||||
class GitProxyException(PeglegBaseException):
|
||||
"""Exception when cloning through proxy."""
|
||||
message = 'Could not resolve proxy [%(location)s]'
|
||||
|
||||
|
||||
class GitSSHException(BaseGitException):
|
||||
class GitSSHException(PeglegBaseException):
|
||||
"""Exception that occurs when an SSH key could not be found."""
|
||||
|
||||
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)
|
||||
message = 'Failed to find specified SSH key: %(ssh_key_path)s'
|
||||
|
||||
|
||||
class GitConfigException(BaseGitException):
|
||||
class GitConfigException(PeglegBaseException):
|
||||
"""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."""
|
||||
message = ("The repository path or URL is invalid: %(repo_path)s")
|
||||
message = 'The repository path or URL is invalid: %(repo_path)s'
|
||||
|
@ -200,15 +200,16 @@ def _try_git_clone(repo_url,
|
||||
ref)
|
||||
if (ssh_cmd and ssh_cmd in e.stderr
|
||||
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():
|
||||
raise exceptions.GitProxyException(proxy_server)
|
||||
raise exceptions.GitProxyException(location=proxy_server)
|
||||
else:
|
||||
raise exceptions.GitException(repo_url, details=e)
|
||||
raise exceptions.GitException(location=repo_url, details=e)
|
||||
except Exception as e:
|
||||
msg = 'Encountered unknown Exception during clone of %s' % repo_url
|
||||
LOG.exception(msg)
|
||||
raise exceptions.GitException(repo_url, details=e)
|
||||
LOG.exception('Encountered unknown Exception during clone of %s',
|
||||
repo_url)
|
||||
raise exceptions.GitException(location=repo_url, details=e)
|
||||
|
||||
_try_git_checkout(repo=repo, repo_url=repo_url, ref=ref)
|
||||
|
||||
@ -241,7 +242,7 @@ def _get_remote_env_vars(auth_key=None):
|
||||
else:
|
||||
msg = "The auth_key path '%s' was not found" % auth_key
|
||||
LOG.error(msg)
|
||||
raise exceptions.GitSSHException(auth_key)
|
||||
raise exceptions.GitSSHException(ssh_key_path=auth_key)
|
||||
return env_vars
|
||||
|
||||
|
||||
@ -297,12 +298,11 @@ def _try_git_checkout(repo, repo_url, ref=None, fetch=True):
|
||||
except git_exc.GitCommandError as e:
|
||||
LOG.exception('Failed to checkout ref=%s from repo_url=%s.', ref,
|
||||
repo_url)
|
||||
raise exceptions.GitException(repo_url, details=e)
|
||||
raise exceptions.GitException(location=repo_url, details=e)
|
||||
except Exception as e:
|
||||
msg = ('Encountered unknown Exception during checkout of ref=%s for '
|
||||
'repo_url=%s' % (ref, repo_url))
|
||||
LOG.exception(msg)
|
||||
raise exceptions.GitException(repo_url, details=e)
|
||||
LOG.exception(('Encountered unknown Exception during checkout of '
|
||||
'ref=%s for repo_url=%s'), ref, repo_url)
|
||||
raise exceptions.GitException(location=repo_url, details=e)
|
||||
|
||||
|
||||
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]):
|
||||
raise exceptions.GitConfigException(repo_url=repo_path)
|
||||
raise exceptions.GitConfigException(repo_path=repo_path)
|
||||
|
||||
# TODO(felipemonteiro): Support this for remote URLs too?
|
||||
repo = Repo(repo_path, search_parent_directories=True)
|
||||
@ -408,9 +408,9 @@ def repo_name(repo_path):
|
||||
else:
|
||||
return repo_url.split('/')[-1]
|
||||
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):
|
||||
|
Loading…
x
Reference in New Issue
Block a user