Show More
@@ -1,215 +1,215 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2014-2018 RhodeCode GmbH |
|
3 | # Copyright (C) 2014-2018 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | """ |
|
21 | """ | |
22 | Custom vcs exceptions module. |
|
22 | Custom vcs exceptions module. | |
23 | """ |
|
23 | """ | |
24 | import logging |
|
24 | import logging | |
25 | import functools |
|
25 | import functools | |
26 | import urllib2 |
|
26 | import urllib2 | |
27 |
|
27 | |||
28 | log = logging.getLogger(__name__) |
|
28 | log = logging.getLogger(__name__) | |
29 |
|
29 | |||
30 |
|
30 | |||
31 | class VCSCommunicationError(Exception): |
|
31 | class VCSCommunicationError(Exception): | |
32 | pass |
|
32 | pass | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | class HttpVCSCommunicationError(VCSCommunicationError): |
|
35 | class HttpVCSCommunicationError(VCSCommunicationError): | |
36 | pass |
|
36 | pass | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | class VCSError(Exception): |
|
39 | class VCSError(Exception): | |
40 | pass |
|
40 | pass | |
41 |
|
41 | |||
42 |
|
42 | |||
43 | class RepositoryError(VCSError): |
|
43 | class RepositoryError(VCSError): | |
44 | pass |
|
44 | pass | |
45 |
|
45 | |||
46 |
|
46 | |||
47 | class RepositoryRequirementError(RepositoryError): |
|
47 | class RepositoryRequirementError(RepositoryError): | |
48 | pass |
|
48 | pass | |
49 |
|
49 | |||
50 |
|
50 | |||
51 | class VCSBackendNotSupportedError(VCSError): |
|
51 | class VCSBackendNotSupportedError(VCSError): | |
52 | """ |
|
52 | """ | |
53 | Exception raised when VCSServer does not support requested backend |
|
53 | Exception raised when VCSServer does not support requested backend | |
54 | """ |
|
54 | """ | |
55 |
|
55 | |||
56 |
|
56 | |||
57 | class EmptyRepositoryError(RepositoryError): |
|
57 | class EmptyRepositoryError(RepositoryError): | |
58 | pass |
|
58 | pass | |
59 |
|
59 | |||
60 |
|
60 | |||
61 | class TagAlreadyExistError(RepositoryError): |
|
61 | class TagAlreadyExistError(RepositoryError): | |
62 | pass |
|
62 | pass | |
63 |
|
63 | |||
64 |
|
64 | |||
65 | class TagDoesNotExistError(RepositoryError): |
|
65 | class TagDoesNotExistError(RepositoryError): | |
66 | pass |
|
66 | pass | |
67 |
|
67 | |||
68 |
|
68 | |||
69 | class BranchAlreadyExistError(RepositoryError): |
|
69 | class BranchAlreadyExistError(RepositoryError): | |
70 | pass |
|
70 | pass | |
71 |
|
71 | |||
72 |
|
72 | |||
73 | class BranchDoesNotExistError(RepositoryError): |
|
73 | class BranchDoesNotExistError(RepositoryError): | |
74 | pass |
|
74 | pass | |
75 |
|
75 | |||
76 |
|
76 | |||
77 | class CommitError(RepositoryError): |
|
77 | class CommitError(RepositoryError): | |
78 | """ |
|
78 | """ | |
79 | Exceptions related to an existing commit |
|
79 | Exceptions related to an existing commit | |
80 | """ |
|
80 | """ | |
81 |
|
81 | |||
82 |
|
82 | |||
83 | class CommitDoesNotExistError(CommitError): |
|
83 | class CommitDoesNotExistError(CommitError): | |
84 | pass |
|
84 | pass | |
85 |
|
85 | |||
86 |
|
86 | |||
87 | class CommittingError(RepositoryError): |
|
87 | class CommittingError(RepositoryError): | |
88 | """ |
|
88 | """ | |
89 | Exceptions happening while creating a new commit |
|
89 | Exceptions happening while creating a new commit | |
90 | """ |
|
90 | """ | |
91 |
|
91 | |||
92 |
|
92 | |||
93 | class NothingChangedError(CommittingError): |
|
93 | class NothingChangedError(CommittingError): | |
94 | pass |
|
94 | pass | |
95 |
|
95 | |||
96 |
|
96 | |||
97 | class NodeError(VCSError): |
|
97 | class NodeError(VCSError): | |
98 | pass |
|
98 | pass | |
99 |
|
99 | |||
100 |
|
100 | |||
101 | class RemovedFileNodeError(NodeError): |
|
101 | class RemovedFileNodeError(NodeError): | |
102 | pass |
|
102 | pass | |
103 |
|
103 | |||
104 |
|
104 | |||
105 | class NodeAlreadyExistsError(CommittingError): |
|
105 | class NodeAlreadyExistsError(CommittingError): | |
106 | pass |
|
106 | pass | |
107 |
|
107 | |||
108 |
|
108 | |||
109 | class NodeAlreadyChangedError(CommittingError): |
|
109 | class NodeAlreadyChangedError(CommittingError): | |
110 | pass |
|
110 | pass | |
111 |
|
111 | |||
112 |
|
112 | |||
113 | class NodeDoesNotExistError(CommittingError): |
|
113 | class NodeDoesNotExistError(CommittingError): | |
114 | pass |
|
114 | pass | |
115 |
|
115 | |||
116 |
|
116 | |||
117 | class NodeNotChangedError(CommittingError): |
|
117 | class NodeNotChangedError(CommittingError): | |
118 | pass |
|
118 | pass | |
119 |
|
119 | |||
120 |
|
120 | |||
121 | class NodeAlreadyAddedError(CommittingError): |
|
121 | class NodeAlreadyAddedError(CommittingError): | |
122 | pass |
|
122 | pass | |
123 |
|
123 | |||
124 |
|
124 | |||
125 | class NodeAlreadyRemovedError(CommittingError): |
|
125 | class NodeAlreadyRemovedError(CommittingError): | |
126 | pass |
|
126 | pass | |
127 |
|
127 | |||
128 |
|
128 | |||
129 | class SubrepoMergeError(RepositoryError): |
|
129 | class SubrepoMergeError(RepositoryError): | |
130 | """ |
|
130 | """ | |
131 | This happens if we try to merge a repository which contains subrepos and |
|
131 | This happens if we try to merge a repository which contains subrepos and | |
132 | the subrepos cannot be merged. The subrepos are not merged itself but |
|
132 | the subrepos cannot be merged. The subrepos are not merged itself but | |
133 | their references in the root repo are merged. |
|
133 | their references in the root repo are merged. | |
134 | """ |
|
134 | """ | |
135 |
|
135 | |||
136 |
|
136 | |||
137 | class ImproperArchiveTypeError(VCSError): |
|
137 | class ImproperArchiveTypeError(VCSError): | |
138 | pass |
|
138 | pass | |
139 |
|
139 | |||
140 |
|
140 | |||
141 | class CommandError(VCSError): |
|
141 | class CommandError(VCSError): | |
142 | pass |
|
142 | pass | |
143 |
|
143 | |||
144 |
|
144 | |||
145 | class UnhandledException(VCSError): |
|
145 | class UnhandledException(VCSError): | |
146 | """ |
|
146 | """ | |
147 | Signals that something unexpected went wrong. |
|
147 | Signals that something unexpected went wrong. | |
148 |
|
148 | |||
149 | This usually means we have a programming error on the side of the VCSServer |
|
149 | This usually means we have a programming error on the side of the VCSServer | |
150 | and should inspect the logfile of the VCSServer to find more details. |
|
150 | and should inspect the logfile of the VCSServer to find more details. | |
151 | """ |
|
151 | """ | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | _EXCEPTION_MAP = { |
|
154 | _EXCEPTION_MAP = { | |
155 | 'abort': RepositoryError, |
|
155 | 'abort': RepositoryError, | |
156 | 'archive': ImproperArchiveTypeError, |
|
156 | 'archive': ImproperArchiveTypeError, | |
157 | 'error': RepositoryError, |
|
157 | 'error': RepositoryError, | |
158 | 'lookup': CommitDoesNotExistError, |
|
158 | 'lookup': CommitDoesNotExistError, | |
159 | 'repo_locked': RepositoryError, |
|
159 | 'repo_locked': RepositoryError, | |
160 | 'requirement': RepositoryRequirementError, |
|
160 | 'requirement': RepositoryRequirementError, | |
161 | 'unhandled': UnhandledException, |
|
161 | 'unhandled': UnhandledException, | |
162 | # TODO: johbo: Define our own exception for this and stop abusing |
|
162 | # TODO: johbo: Define our own exception for this and stop abusing | |
163 | # urllib's exception class. |
|
163 | # urllib's exception class. | |
164 | 'url_error': urllib2.URLError, |
|
164 | 'url_error': urllib2.URLError, | |
165 | 'subrepo_merge_error': SubrepoMergeError, |
|
165 | 'subrepo_merge_error': SubrepoMergeError, | |
166 | } |
|
166 | } | |
167 |
|
167 | |||
168 |
|
168 | |||
169 | def map_vcs_exceptions(func): |
|
169 | def map_vcs_exceptions(func): | |
170 | """ |
|
170 | """ | |
171 | Utility to decorate functions so that plain exceptions are translated. |
|
171 | Utility to decorate functions so that plain exceptions are translated. | |
172 |
|
172 | |||
173 | The translation is based on `exc_map` which maps a `str` indicating |
|
173 | The translation is based on `exc_map` which maps a `str` indicating | |
174 | the error type into an exception class representing this error inside |
|
174 | the error type into an exception class representing this error inside | |
175 | of the vcs layer. |
|
175 | of the vcs layer. | |
176 | """ |
|
176 | """ | |
177 |
|
177 | |||
178 | @functools.wraps(func) |
|
178 | @functools.wraps(func) | |
179 | def wrapper(*args, **kwargs): |
|
179 | def wrapper(*args, **kwargs): | |
180 | try: |
|
180 | try: | |
181 | return func(*args, **kwargs) |
|
181 | return func(*args, **kwargs) | |
182 | except Exception as e: |
|
182 | except Exception as e: | |
183 | # The error middleware adds information if it finds |
|
183 | # The error middleware adds information if it finds | |
184 | # __traceback_info__ in a frame object. This way the remote |
|
184 | # __traceback_info__ in a frame object. This way the remote | |
185 | # traceback information is made available in error reports. |
|
185 | # traceback information is made available in error reports. | |
186 | remote_tb = getattr(e, '_vcs_server_traceback', None) |
|
186 | remote_tb = getattr(e, '_vcs_server_traceback', None) | |
187 | __traceback_info__ = None |
|
187 | __traceback_info__ = None | |
188 | if remote_tb: |
|
188 | if remote_tb: | |
189 | if isinstance(remote_tb, basestring): |
|
189 | if isinstance(remote_tb, basestring): | |
190 | remote_tb = [remote_tb] |
|
190 | remote_tb = [remote_tb] | |
191 | __traceback_info__ = ( |
|
191 | __traceback_info__ = ( | |
192 | 'Found VCSServer remote traceback information:\n\n' + |
|
192 | 'Found VCSServer remote traceback information:\n\n' + | |
193 | '\n'.join(remote_tb)) |
|
193 | '\n'.join(remote_tb)) | |
194 |
|
194 | |||
195 | # Avoid that remote_tb also appears in the frame |
|
195 | # Avoid that remote_tb also appears in the frame | |
196 | del remote_tb |
|
196 | del remote_tb | |
197 |
|
197 | |||
198 | # Special vcs errors had an attribute "_vcs_kind" which is used |
|
198 | # Special vcs errors had an attribute "_vcs_kind" which is used | |
199 | # to translate them to the proper exception class in the vcs |
|
199 | # to translate them to the proper exception class in the vcs | |
200 | # client layer. |
|
200 | # client layer. | |
201 | kind = getattr(e, '_vcs_kind', None) |
|
201 | kind = getattr(e, '_vcs_kind', None) | |
202 |
|
202 | |||
203 | if kind: |
|
203 | if kind: | |
204 | if any(e.args): |
|
204 | if any(e.args): | |
205 | args = e.args |
|
205 | args = e.args | |
206 | else: |
|
206 | else: | |
207 | args = [__traceback_info__ or 'unhandledException'] |
|
207 | args = [__traceback_info__ or 'unhandledException'] | |
208 |
if __traceback_info__ and kind |
|
208 | if __traceback_info__ and kind not in ['unhandled', 'lookup']: | |
209 | # for other than unhandled errors also log the traceback |
|
209 | # for other than unhandled errors also log the traceback | |
210 | # can be usefull for debugging |
|
210 | # can be usefull for debugging | |
211 | log.error(__traceback_info__) |
|
211 | log.error(__traceback_info__) | |
212 | raise _EXCEPTION_MAP[kind](*args) |
|
212 | raise _EXCEPTION_MAP[kind](*args) | |
213 | else: |
|
213 | else: | |
214 | raise |
|
214 | raise | |
215 | return wrapper |
|
215 | return wrapper |
General Comments 0
You need to be logged in to leave comments.
Login now