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