Browse Source

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>
Tin Lam 5 months ago
parent
commit
ae81ade94d
3 changed files with 50 additions and 73 deletions
  1. 0
    5
      doc/source/exceptions.rst
  2. 35
    53
      pegleg/engine/exceptions.py
  3. 15
    15
      pegleg/engine/util/git.py

+ 0
- 5
doc/source/exceptions.rst View File

@@ -27,11 +27,6 @@ Base Exceptions
27 27
 Git Exceptions
28 28
 --------------
29 29
 
30
-.. autoexception:: pegleg.engine.exceptions.BaseGitException
31
-   :members:
32
-   :show-inheritance:
33
-   :undoc-members:
34
-
35 30
 .. autoexception:: pegleg.engine.exceptions.GitConfigException
36 31
    :members:
37 32
    :show-inheritance:

+ 35
- 53
pegleg/engine/exceptions.py View File

@@ -12,78 +12,60 @@
12 12
 # See the License for the specific language governing permissions and
13 13
 # limitations under the License.
14 14
 
15
+import logging
15 16
 
16
-class PeglegBaseException(Exception):
17
-    """Base class for Pegleg exception and error handling."""
17
+__all__ = ('PeglegBaseException',
18
+           'GitException',
19
+           'GitAuthException',
20
+           'GitProxyException',
21
+           'GitSSHException',
22
+           'GitConfigException',
23
+           'GitInvalidRepoException')
18 24
 
19
-    def __init__(self, message=None, **kwargs):
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)
25
+LOG = logging.getLogger(__name__)
26 26
 
27 27
 
28
-class BaseGitException(PeglegBaseException):
29
-    """Base class for Git exceptions and error handling."""
30
-
31
-    message = 'An unknown error occurred while accessing a chart source.'
28
+class PeglegBaseException(Exception):
29
+    """The base Pegleg exception for everything."""
32 30
 
31
+    message = "Base Pegleg exception"
33 32
 
34
-class GitException(BaseGitException):
35
-    """Exception when an error occurs cloning a Git repository."""
33
+    def __init__(self, message=None, **kwargs):
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)
42
+class 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
 
46
-class GitAuthException(BaseGitException):
48
+class GitAuthException(PeglegBaseException):
47 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 '
54
-                         'path %s' % (self._repo_url, self._ssh_key_path))
54
+class GitProxyException(PeglegBaseException):
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
-
59
-class 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
-
70
-class GitSSHException(BaseGitException):
59
+class GitSSHException(PeglegBaseException):
71 60
     """Exception that occurs when an SSH key could not be found."""
72
-
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)
61
+    message = 'Failed to find specified SSH key: %(ssh_key_path)s'
80 62
 
81 63
 
82
-class GitConfigException(BaseGitException):
64
+class GitConfigException(PeglegBaseException):
83 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
 
87
-class GitInvalidRepoException(BaseGitException):
69
+class GitInvalidRepoException(PeglegBaseException):
88 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'

+ 15
- 15
pegleg/engine/util/git.py View File

@@ -200,15 +200,16 @@ def _try_git_clone(repo_url,
200 200
                       ref)
201 201
         if (ssh_cmd and ssh_cmd in e.stderr
202 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 205
         elif 'could not resolve proxy' in e.stderr.lower():
205
-            raise exceptions.GitProxyException(proxy_server)
206
+            raise exceptions.GitProxyException(location=proxy_server)
206 207
         else:
207
-            raise exceptions.GitException(repo_url, details=e)
208
+            raise exceptions.GitException(location=repo_url, details=e)
208 209
     except Exception as e:
209
-        msg = 'Encountered unknown Exception during clone of %s' % repo_url
210
-        LOG.exception(msg)
211
-        raise exceptions.GitException(repo_url, details=e)
210
+        LOG.exception('Encountered unknown Exception during clone of %s',
211
+                      repo_url)
212
+        raise exceptions.GitException(location=repo_url, details=e)
212 213
 
213 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 242
         else:
242 243
             msg = "The auth_key path '%s' was not found" % auth_key
243 244
             LOG.error(msg)
244
-            raise exceptions.GitSSHException(auth_key)
245
+            raise exceptions.GitSSHException(ssh_key_path=auth_key)
245 246
     return env_vars
246 247
 
247 248
 
@@ -297,12 +298,11 @@ def _try_git_checkout(repo, repo_url, ref=None, fetch=True):
297 298
     except git_exc.GitCommandError as e:
298 299
         LOG.exception('Failed to checkout ref=%s from repo_url=%s.', ref,
299 300
                       repo_url)
300
-        raise exceptions.GitException(repo_url, details=e)
301
+        raise exceptions.GitException(location=repo_url, details=e)
301 302
     except Exception as e:
302
-        msg = ('Encountered unknown Exception during checkout of ref=%s for '
303
-               'repo_url=%s' % (ref, repo_url))
304
-        LOG.exception(msg)
305
-        raise exceptions.GitException(repo_url, details=e)
303
+        LOG.exception(('Encountered unknown Exception during checkout of '
304
+                       'ref=%s for repo_url=%s'), ref, repo_url)
305
+        raise exceptions.GitException(location=repo_url, details=e)
306 306
 
307 307
 
308 308
 def _create_or_checkout_local_ref(g, branches, ref):
@@ -388,7 +388,7 @@ def repo_name(repo_path):
388 388
     """
389 389
 
390 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 393
     # TODO(felipemonteiro): Support this for remote URLs too?
394 394
     repo = Repo(repo_path, search_parent_directories=True)
@@ -408,9 +408,9 @@ def repo_name(repo_path):
408 408
                 else:
409 409
                     return repo_url.split('/')[-1]
410 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
 
416 416
 def normalize_repo_path(repo_url_or_path):

Loading…
Cancel
Save