summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.openstack.org>2018-11-13 16:50:49 +0000
committerGerrit Code Review <review@openstack.org>2018-11-13 16:50:49 +0000
commitf6cfb1cf2a3d38b71ec5d9685a220c47ac2c8050 (patch)
tree92d0a0f7a81781331e3ab1c21d865c741c9eaa66
parentec2ed74f05920992648e966de88df8b9e40ac75c (diff)
parentae81ade94dd6086ee82c5daab3744e3a480a10e3 (diff)
Merge "Refactor Pegleg exceptions"
-rw-r--r--doc/source/exceptions.rst5
-rw-r--r--pegleg/engine/exceptions.py88
-rw-r--r--pegleg/engine/util/git.py30
3 files changed, 50 insertions, 73 deletions
diff --git a/doc/source/exceptions.rst b/doc/source/exceptions.rst
index f23ef1d..a0c5450 100644
--- a/doc/source/exceptions.rst
+++ b/doc/source/exceptions.rst
@@ -27,11 +27,6 @@ Base Exceptions
27Git Exceptions 27Git Exceptions
28-------------- 28--------------
29 29
30.. autoexception:: pegleg.engine.exceptions.BaseGitException
31 :members:
32 :show-inheritance:
33 :undoc-members:
34
35.. autoexception:: pegleg.engine.exceptions.GitConfigException 30.. autoexception:: pegleg.engine.exceptions.GitConfigException
36 :members: 31 :members:
37 :show-inheritance: 32 :show-inheritance:
diff --git a/pegleg/engine/exceptions.py b/pegleg/engine/exceptions.py
index ae5adaf..8fc8e0e 100644
--- a/pegleg/engine/exceptions.py
+++ b/pegleg/engine/exceptions.py
@@ -12,78 +12,60 @@
12# See the License for the specific language governing permissions and 12# See the License for the specific language governing permissions and
13# limitations under the License. 13# limitations under the License.
14 14
15import logging
15 16
16class PeglegBaseException(Exception): 17__all__ = ('PeglegBaseException',
17 """Base class for Pegleg exception and error handling.""" 18 'GitException',
19 'GitAuthException',
20 'GitProxyException',
21 'GitSSHException',
22 'GitConfigException',
23 'GitInvalidRepoException')
18 24
19 def __init__(self, message=None, **kwargs): 25LOG = logging.getLogger(__name__)
20 self.message = message or self.message
21 try: # nosec
22 self.message = self.message % kwargs
23 except Exception:
24 pass
25 super(PeglegBaseException, self).__init__(self.message)
26 26
27 27
28class BaseGitException(PeglegBaseException): 28class PeglegBaseException(Exception):
29 """Base class for Git exceptions and error handling.""" 29 """The base Pegleg exception for everything."""
30
31 message = 'An unknown error occurred while accessing a chart source.'
32 30
31 message = "Base Pegleg exception"
33 32
34class GitException(BaseGitException): 33 def __init__(self, message=None, **kwargs):
35 """Exception when an error occurs cloning a Git repository.""" 34 self.message = message or self.message
35 try:
36 self.message = self.message.format(**kwargs)
37 except KeyError:
38 LOG.warning("Missing kwargs")
39 super().__init__(self.message)
36 40
37 def __init__(self, location, details=None):
38 self._message = ('Git exception occurred: [%s] may not be a valid git '
39 'repository' % location)
40 if details:
41 self._message += '. Details: %s' % details
42 41
43 super(GitException, self).__init__(self._message) 42class GitException(PeglegBaseException):
43 """Exception when an error occurs cloning a Git repository."""
44 message = ('Git exception occurred: [%(location)s] may not be a valid '
45 'git repository. Details: %(details)s')
44 46
45 47
46class GitAuthException(BaseGitException): 48class GitAuthException(PeglegBaseException):
47 """Exception that occurs when authentication fails for cloning a repo.""" 49 """Exception that occurs when authentication fails for cloning a repo."""
50 message = ('Failed to authenticate for repo %(repo_url)s with ssh-key '
51 'at path %(ssh_key_path)s')
48 52
49 def __init__(self, repo_url, ssh_key_path):
50 self._repo_url = repo_url
51 self._ssh_key_path = ssh_key_path
52 53
53 self._message = ('Failed to authenticate for repo %s with ssh-key at ' 54class GitProxyException(PeglegBaseException):
54 'path %s' % (self._repo_url, self._ssh_key_path)) 55 """Exception when cloning through proxy."""
56 message = 'Could not resolve proxy [%(location)s]'
55 57
56 super(GitAuthException, self).__init__(self._message)
57 58
58 59class GitSSHException(PeglegBaseException):
59class GitProxyException(BaseGitException):
60 """Exception when an error occurs cloning a Git repository
61 through a proxy."""
62
63 def __init__(self, location):
64 self._location = location
65 self._message = ('Could not resolve proxy [%s]' % self._location)
66
67 super(GitProxyException, self).__init__(self._message)
68
69
70class GitSSHException(BaseGitException):
71 """Exception that occurs when an SSH key could not be found.""" 60 """Exception that occurs when an SSH key could not be found."""
72 61 message = 'Failed to find specified SSH key: %(ssh_key_path)s'
73 def __init__(self, ssh_key_path):
74 self._ssh_key_path = ssh_key_path
75
76 self._message = ('Failed to find specified SSH key: %s' %
77 (self._ssh_key_path))
78
79 super(GitSSHException, self).__init__(self._message)
80 62
81 63
82class GitConfigException(BaseGitException): 64class GitConfigException(PeglegBaseException):
83 """Exception that occurs when reading Git repo config fails.""" 65 """Exception that occurs when reading Git repo config fails."""
84 message = ("Failed to read Git config file for repo path: %(repo_path)s") 66 message = 'Failed to read Git config file for repo path: %(repo_path)s'
85 67
86 68
87class GitInvalidRepoException(BaseGitException): 69class GitInvalidRepoException(PeglegBaseException):
88 """Exception raised when an invalid repository is detected.""" 70 """Exception raised when an invalid repository is detected."""
89 message = ("The repository path or URL is invalid: %(repo_path)s") 71 message = 'The repository path or URL is invalid: %(repo_path)s'
diff --git a/pegleg/engine/util/git.py b/pegleg/engine/util/git.py
index dce3f96..b1ab65f 100644
--- a/pegleg/engine/util/git.py
+++ b/pegleg/engine/util/git.py
@@ -200,15 +200,16 @@ def _try_git_clone(repo_url,
200 ref) 200 ref)
201 if (ssh_cmd and ssh_cmd in e.stderr 201 if (ssh_cmd and ssh_cmd in e.stderr
202 or 'permission denied' in e.stderr.lower()): 202 or 'permission denied' in e.stderr.lower()):
203 raise exceptions.GitAuthException(repo_url, auth_key) 203 raise exceptions.GitAuthException(
204 repo_url=repo_url, ssh_key_path=auth_key)
204 elif 'could not resolve proxy' in e.stderr.lower(): 205 elif 'could not resolve proxy' in e.stderr.lower():
205 raise exceptions.GitProxyException(proxy_server) 206 raise exceptions.GitProxyException(location=proxy_server)
206 else: 207 else:
207 raise exceptions.GitException(repo_url, details=e) 208 raise exceptions.GitException(location=repo_url, details=e)
208 except Exception as e: 209 except Exception as e:
209 msg = 'Encountered unknown Exception during clone of %s' % repo_url 210 LOG.exception('Encountered unknown Exception during clone of %s',
210 LOG.exception(msg) 211 repo_url)
211 raise exceptions.GitException(repo_url, details=e) 212 raise exceptions.GitException(location=repo_url, details=e)
212 213
213 _try_git_checkout(repo=repo, repo_url=repo_url, ref=ref) 214 _try_git_checkout(repo=repo, repo_url=repo_url, ref=ref)
214 215
@@ -241,7 +242,7 @@ def _get_remote_env_vars(auth_key=None):
241 else: 242 else:
242 msg = "The auth_key path '%s' was not found" % auth_key 243 msg = "The auth_key path '%s' was not found" % auth_key
243 LOG.error(msg) 244 LOG.error(msg)
244 raise exceptions.GitSSHException(auth_key) 245 raise exceptions.GitSSHException(ssh_key_path=auth_key)
245 return env_vars 246 return env_vars
246 247
247 248
@@ -297,12 +298,11 @@ def _try_git_checkout(repo, repo_url, ref=None, fetch=True):
297 except git_exc.GitCommandError as e: 298 except git_exc.GitCommandError as e:
298 LOG.exception('Failed to checkout ref=%s from repo_url=%s.', ref, 299 LOG.exception('Failed to checkout ref=%s from repo_url=%s.', ref,
299 repo_url) 300 repo_url)
300 raise exceptions.GitException(repo_url, details=e) 301 raise exceptions.GitException(location=repo_url, details=e)
301 except Exception as e: 302 except Exception as e:
302 msg = ('Encountered unknown Exception during checkout of ref=%s for ' 303 LOG.exception(('Encountered unknown Exception during checkout of '
303 'repo_url=%s' % (ref, repo_url)) 304 'ref=%s for repo_url=%s'), ref, repo_url)
304 LOG.exception(msg) 305 raise exceptions.GitException(location=repo_url, details=e)
305 raise exceptions.GitException(repo_url, details=e)
306 306
307 307
308def _create_or_checkout_local_ref(g, branches, ref): 308def _create_or_checkout_local_ref(g, branches, ref):
@@ -388,7 +388,7 @@ def repo_name(repo_path):
388 """ 388 """
389 389
390 if not is_repository(normalize_repo_path(repo_path)[0]): 390 if not is_repository(normalize_repo_path(repo_path)[0]):
391 raise exceptions.GitConfigException(repo_url=repo_path) 391 raise exceptions.GitConfigException(repo_path=repo_path)
392 392
393 # TODO(felipemonteiro): Support this for remote URLs too? 393 # TODO(felipemonteiro): Support this for remote URLs too?
394 repo = Repo(repo_path, search_parent_directories=True) 394 repo = Repo(repo_path, search_parent_directories=True)
@@ -408,9 +408,9 @@ def repo_name(repo_path):
408 else: 408 else:
409 return repo_url.split('/')[-1] 409 return repo_url.split('/')[-1]
410 except Exception: 410 except Exception:
411 raise exceptions.GitConfigException(repo_url=repo_path) 411 raise exceptions.GitConfigException(repo_path=repo_path)
412 412
413 raise exceptions.GitConfigException(repo_url=repo_path) 413 raise exceptions.GitConfigException(repo_path=repo_path)
414 414
415 415
416def normalize_repo_path(repo_url_or_path): 416def normalize_repo_path(repo_url_or_path):