##// END OF EJS Templates
exceptions: use new wrapper that store the org exception inside the newly generated exceptions....
marcink -
r490:2961b1db default
parent child Browse files
Show More
@@ -28,7 +28,7 b' import functools'
28 28 from pyramid.httpexceptions import HTTPLocked
29 29
30 30
31 def _make_exception(kind, *args):
31 def _make_exception(kind, org_exc, *args):
32 32 """
33 33 Prepares a base `Exception` instance to be sent over the wire.
34 34
@@ -37,26 +37,62 b' def _make_exception(kind, *args):'
37 37 """
38 38 exc = Exception(*args)
39 39 exc._vcs_kind = kind
40 exc._org_exc = org_exc
40 41 return exc
41 42
42 43
43 AbortException = functools.partial(_make_exception, 'abort')
44 def AbortException(org_exc=None):
45 def _make_exception_wrapper(*args):
46 return _make_exception('abort', org_exc, *args)
47 return _make_exception_wrapper
48
44 49
45 ArchiveException = functools.partial(_make_exception, 'archive')
50 def ArchiveException(org_exc=None):
51 def _make_exception_wrapper(*args):
52 return _make_exception('archive', org_exc, *args)
53 return _make_exception_wrapper
54
46 55
47 LookupException = functools.partial(_make_exception, 'lookup')
56 def LookupException(org_exc=None):
57 def _make_exception_wrapper(*args):
58 return _make_exception('lookup', org_exc, *args)
59 return _make_exception_wrapper
60
48 61
49 VcsException = functools.partial(_make_exception, 'error')
62 def VcsException(org_exc=None):
63 def _make_exception_wrapper(*args):
64 return _make_exception('error', org_exc, *args)
65 return _make_exception_wrapper
66
50 67
51 RepositoryLockedException = functools.partial(_make_exception, 'repo_locked')
68 def RepositoryLockedException(org_exc=None):
69 def _make_exception_wrapper(*args):
70 return _make_exception('repo_locked', org_exc, *args)
71 return _make_exception_wrapper
72
52 73
53 RequirementException = functools.partial(_make_exception, 'requirement')
74 def RequirementException(org_exc=None):
75 def _make_exception_wrapper(*args):
76 return _make_exception('requirement', org_exc, *args)
77 return _make_exception_wrapper
78
54 79
55 UnhandledException = functools.partial(_make_exception, 'unhandled')
80 def UnhandledException(org_exc=None):
81 def _make_exception_wrapper(*args):
82 return _make_exception('unhandled', org_exc, *args)
83 return _make_exception_wrapper
84
56 85
57 URLError = functools.partial(_make_exception, 'url_error')
86 def URLError(org_exc=None):
87 def _make_exception_wrapper(*args):
88 return _make_exception('url_error', org_exc, *args)
89 return _make_exception_wrapper
58 90
59 SubrepoMergeException = functools.partial(_make_exception, 'subrepo_merge_error')
91
92 def SubrepoMergeException(org_exc=None):
93 def _make_exception_wrapper(*args):
94 return _make_exception('subrepo_merge_error', org_exc, *args)
95 return _make_exception_wrapper
60 96
61 97
62 98 class HTTPRepoLocked(HTTPLocked):
@@ -56,9 +56,9 b' def reraise_safe_exceptions(func):'
56 56 return func(*args, **kwargs)
57 57 except (ChecksumMismatch, WrongObjectException, MissingCommitError,
58 58 ObjectMissing) as e:
59 raise exceptions.LookupException(e.message)
59 raise exceptions.LookupException(e)(e.message)
60 60 except (HangupException, UnexpectedCommandError) as e:
61 raise exceptions.VcsException(e.message)
61 raise exceptions.VcsException(e)(e.message)
62 62 except Exception as e:
63 63 # NOTE(marcink): becuase of how dulwich handles some exceptions
64 64 # (KeyError on empty repos), we cannot track this and catch all
@@ -214,8 +214,8 b' class GitRemote(object):'
214 214 elif attr in ["author", "message", "parents"]:
215 215 args.append(attr)
216 216 result[attr] = method(*args)
217 except KeyError:
218 raise exceptions.VcsException(
217 except KeyError as e:
218 raise exceptions.VcsException(e)(
219 219 "Unknown bulk attribute: %s" % attr)
220 220 return result
221 221
@@ -258,11 +258,11 b' class GitRemote(object):'
258 258 log.debug("Trying to open URL %s", cleaned_uri)
259 259 resp = o.open(req)
260 260 if resp.code != 200:
261 raise exceptions.URLError('Return Code is not 200')
261 raise exceptions.URLError()('Return Code is not 200')
262 262 except Exception as e:
263 263 log.warning("URL cannot be opened: %s", cleaned_uri, exc_info=True)
264 264 # means it cannot be cloned
265 raise exceptions.URLError("[%s] org_exc: %s" % (cleaned_uri, e))
265 raise exceptions.URLError(e)("[%s] org_exc: %s" % (cleaned_uri, e))
266 266
267 267 # now detect if it's proper git repo
268 268 gitdata = resp.read()
@@ -272,7 +272,7 b' class GitRemote(object):'
272 272 # old style git can return some other format !
273 273 pass
274 274 else:
275 raise exceptions.URLError(
275 raise exceptions.URLError()(
276 276 "url [%s] does not look like an git" % (cleaned_uri,))
277 277
278 278 return True
@@ -419,7 +419,7 b' class GitRemote(object):'
419 419 log.warning(
420 420 'Trying to fetch from "%s" failed, not a Git repository.', url)
421 421 # Exception can contain unicode which we convert
422 raise exceptions.AbortException(repr(e))
422 raise exceptions.AbortException(e)(repr(e))
423 423
424 424 # mikhail: client.fetch() returns all the remote refs, but fetches only
425 425 # refs filtered by `determine_wants` function. We need to filter result
@@ -655,7 +655,7 b' class GitRemote(object):'
655 655 if safe_call:
656 656 return '', err
657 657 else:
658 raise exceptions.VcsException(tb_err)
658 raise exceptions.VcsException()(tb_err)
659 659
660 660 @reraise_safe_exceptions
661 661 def install_hooks(self, wire, force=False):
@@ -74,20 +74,21 b' def reraise_safe_exceptions(func):'
74 74 def wrapper(*args, **kwargs):
75 75 try:
76 76 return func(*args, **kwargs)
77 except (Abort, InterventionRequired):
78 raise_from_original(exceptions.AbortException)
79 except RepoLookupError:
80 raise_from_original(exceptions.LookupException)
81 except RequirementError:
82 raise_from_original(exceptions.RequirementException)
83 except RepoError:
84 raise_from_original(exceptions.VcsException)
85 except LookupError:
86 raise_from_original(exceptions.LookupException)
77 except (Abort, InterventionRequired) as e:
78 raise_from_original(exceptions.AbortException(e))
79 except RepoLookupError as e:
80 raise_from_original(exceptions.LookupException(e))
81 except RequirementError as e:
82 raise_from_original(exceptions.RequirementException(e))
83 except RepoError as e:
84 raise_from_original(exceptions.VcsException(e))
85 except LookupError as e:
86 raise_from_original(exceptions.LookupException(e))
87 87 except Exception as e:
88 88 if not hasattr(e, '_vcs_kind'):
89 89 log.exception("Unhandled exception in hg remote call")
90 raise_from_original(exceptions.UnhandledException)
90 raise_from_original(exceptions.UnhandledException(e))
91
91 92 raise
92 93 return wrapper
93 94
@@ -149,7 +150,7 b' class HgRemote(object):'
149 150 elif kind == 'zip':
150 151 archiver = archival.zipit(archive_path, mtime)
151 152 else:
152 raise exceptions.ArchiveException(
153 raise exceptions.ArchiveException()(
153 154 'Remote does not support: "%s".' % kind)
154 155
155 156 for f_path, f_mode, f_is_link, f_content in file_info:
@@ -181,8 +182,8 b' class HgRemote(object):'
181 182 try:
182 183 method = self._bulk_methods[attr]
183 184 result[attr] = method(wire, rev)
184 except KeyError:
185 raise exceptions.VcsException(
185 except KeyError as e:
186 raise exceptions.VcsException(e)(
186 187 'Unknown bulk attribute: "%s"' % attr)
187 188 return result
188 189
@@ -219,7 +220,7 b' class HgRemote(object):'
219 220 isexec=bool(node['mode'] & stat.S_IXUSR),
220 221 copied=False)
221 222
222 raise exceptions.AbortException(
223 raise exceptions.AbortException()(
223 224 "Given path haven't been marked as added, "
224 225 "changed or removed (%s)" % path)
225 226
@@ -369,11 +370,11 b' class HgRemote(object):'
369 370 log.debug("Trying to open URL %s", cleaned_uri)
370 371 resp = o.open(req)
371 372 if resp.code != 200:
372 raise exceptions.URLError('Return Code is not 200')
373 raise exceptions.URLError()('Return Code is not 200')
373 374 except Exception as e:
374 375 log.warning("URL cannot be opened: %s", cleaned_uri, exc_info=True)
375 376 # means it cannot be cloned
376 raise exceptions.URLError("[%s] org_exc: %s" % (cleaned_uri, e))
377 raise exceptions.URLError(e)("[%s] org_exc: %s" % (cleaned_uri, e))
377 378
378 379 # now check if it's a proper hg repo, but don't do it for svn
379 380 try:
@@ -390,7 +391,7 b' class HgRemote(object):'
390 391 except Exception as e:
391 392 log.warning("URL is not a valid Mercurial repository: %s",
392 393 cleaned_uri)
393 raise exceptions.URLError(
394 raise exceptions.URLError(e)(
394 395 "url [%s] does not look like an hg repo org_exc: %s"
395 396 % (cleaned_uri, e))
396 397
@@ -412,8 +413,8 b' class HgRemote(object):'
412 413 try:
413 414 return "".join(patch.diff(
414 415 repo, node1=rev1, node2=rev2, match=match_filter, opts=opts))
415 except RepoLookupError:
416 raise exceptions.LookupException()
416 except RepoLookupError as e:
417 raise exceptions.LookupException(e)()
417 418
418 419 @reraise_safe_exceptions
419 420 def file_history(self, wire, revision, path, limit):
@@ -555,10 +556,10 b' class HgRemote(object):'
555 556
556 557 try:
557 558 ctx = repo[revision]
558 except RepoLookupError:
559 raise exceptions.LookupException(revision)
559 except RepoLookupError as e:
560 raise exceptions.LookupException(e)(revision)
560 561 except LookupError as e:
561 raise exceptions.LookupException(e.name)
562 raise exceptions.LookupException(e)(e.name)
562 563
563 564 if not both:
564 565 return ctx.hex()
@@ -674,7 +675,7 b' class HgRemote(object):'
674 675 except Abort as e:
675 676 log.exception("Tag operation aborted")
676 677 # Exception can contain unicode which we convert
677 raise exceptions.AbortException(repr(e))
678 raise exceptions.AbortException(e)(repr(e))
678 679
679 680 @reraise_safe_exceptions
680 681 def tags(self, wire):
@@ -116,7 +116,7 b' def patch_subrepo_type_mapping():'
116 116
117 117 def merge(self, state):
118 118 """merge currently-saved state with the new state."""
119 raise SubrepoMergeException()
119 raise SubrepoMergeException()()
120 120
121 121 def push(self, opts):
122 122 """perform whatever action is analogous to 'hg push'
@@ -120,9 +120,9 b' def _handle_exception(result):'
120 120 log.error('Got traceback from remote call:%s', exception_traceback)
121 121
122 122 if exception_class == 'HTTPLockedRC':
123 raise exceptions.RepositoryLockedException(*result['exception_args'])
123 raise exceptions.RepositoryLockedException()(*result['exception_args'])
124 124 elif exception_class == 'RepositoryError':
125 raise exceptions.VcsException(*result['exception_args'])
125 raise exceptions.VcsException()(*result['exception_args'])
126 126 elif exception_class:
127 127 raise Exception('Got remote exception "%s" with args "%s"' %
128 128 (exception_class, result['exception_args']))
@@ -152,8 +152,8 b' def create_hg_wsgi_app(repo_path, repo_n'
152 152
153 153 try:
154 154 return HgWeb(repo_path, name=repo_name, baseui=baseui)
155 except mercurial.error.RequirementError as exc:
156 raise exceptions.RequirementException(exc)
155 except mercurial.error.RequirementError as e:
156 raise exceptions.RequirementException(e)(e)
157 157
158 158
159 159 class GitHandler(object):
@@ -64,8 +64,8 b' def reraise_safe_exceptions(func):'
64 64 return func(*args, **kwargs)
65 65 except Exception as e:
66 66 if not hasattr(e, '_vcs_kind'):
67 log.exception("Unhandled exception in hg remote call")
68 raise_from_original(exceptions.UnhandledException)
67 log.exception("Unhandled exception in svn remote call")
68 raise_from_original(exceptions.UnhandledException(e))
69 69 raise
70 70 return wrapper
71 71
@@ -120,7 +120,7 b' class TestReraiseSafeExceptions(object):'
120 120 def test_does_not_map_known_exceptions(self):
121 121 @hg.reraise_safe_exceptions
122 122 def stub_method():
123 raise exceptions.LookupException('stub')
123 raise exceptions.LookupException()('stub')
124 124
125 125 with pytest.raises(Exception) as exc_info:
126 126 stub_method()
General Comments 0
You need to be logged in to leave comments. Login now