##// END OF EJS Templates
dispatch: move part of callcatch to scmutil...
Jun Wu -
r30520:4338f87d default
parent child Browse files
Show More
@@ -15,7 +15,6 b' import pdb'
15 import re
15 import re
16 import shlex
16 import shlex
17 import signal
17 import signal
18 import socket
19 import sys
18 import sys
20 import time
19 import time
21 import traceback
20 import traceback
@@ -38,6 +37,7 b' from . import ('
38 profiling,
37 profiling,
39 pycompat,
38 pycompat,
40 revset,
39 revset,
40 scmutil,
41 templatefilters,
41 templatefilters,
42 templatekw,
42 templatekw,
43 templater,
43 templater,
@@ -218,30 +218,15 b' def _runcatch(req):'
218 return callcatch(ui, _runcatchfunc)
218 return callcatch(ui, _runcatchfunc)
219
219
220 def callcatch(ui, func):
220 def callcatch(ui, func):
221 """call func() with global exception handling
221 """like scmutil.callcatch but handles more high-level exceptions about
222
222 config parsing and commands. besides, use handlecommandexception to handle
223 return func() if no exception happens. otherwise do some error handling
223 uncaught exceptions.
224 and return an exit code accordingly.
225 """
224 """
226 try:
225 try:
227 return func()
226 return scmutil.callcatch(ui, func)
228 # Global exception handling, alphabetically
229 # Mercurial-specific first, followed by built-in and library exceptions
230 except error.AmbiguousCommand as inst:
227 except error.AmbiguousCommand as inst:
231 ui.warn(_("hg: command '%s' is ambiguous:\n %s\n") %
228 ui.warn(_("hg: command '%s' is ambiguous:\n %s\n") %
232 (inst.args[0], " ".join(inst.args[1])))
229 (inst.args[0], " ".join(inst.args[1])))
233 except error.ParseError as inst:
234 _formatparse(ui.warn, inst)
235 return -1
236 except error.LockHeld as inst:
237 if inst.errno == errno.ETIMEDOUT:
238 reason = _('timed out waiting for lock held by %s') % inst.locker
239 else:
240 reason = _('lock held by %s') % inst.locker
241 ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason))
242 except error.LockUnavailable as inst:
243 ui.warn(_("abort: could not lock %s: %s\n") %
244 (inst.desc or inst.filename, inst.strerror))
245 except error.CommandError as inst:
230 except error.CommandError as inst:
246 if inst.args[0]:
231 if inst.args[0]:
247 ui.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1]))
232 ui.warn(_("hg %s: %s\n") % (inst.args[0], inst.args[1]))
@@ -249,34 +234,9 b' def callcatch(ui, func):'
249 else:
234 else:
250 ui.warn(_("hg: %s\n") % inst.args[1])
235 ui.warn(_("hg: %s\n") % inst.args[1])
251 commands.help_(ui, 'shortlist')
236 commands.help_(ui, 'shortlist')
252 except error.OutOfBandError as inst:
237 except error.ParseError as inst:
253 if inst.args:
238 _formatparse(ui.warn, inst)
254 msg = _("abort: remote error:\n")
239 return -1
255 else:
256 msg = _("abort: remote error\n")
257 ui.warn(msg)
258 if inst.args:
259 ui.warn(''.join(inst.args))
260 if inst.hint:
261 ui.warn('(%s)\n' % inst.hint)
262 except error.RepoError as inst:
263 ui.warn(_("abort: %s!\n") % inst)
264 if inst.hint:
265 ui.warn(_("(%s)\n") % inst.hint)
266 except error.ResponseError as inst:
267 ui.warn(_("abort: %s") % inst.args[0])
268 if not isinstance(inst.args[1], basestring):
269 ui.warn(" %r\n" % (inst.args[1],))
270 elif not inst.args[1]:
271 ui.warn(_(" empty string\n"))
272 else:
273 ui.warn("\n%r\n" % util.ellipsis(inst.args[1]))
274 except error.CensoredNodeError as inst:
275 ui.warn(_("abort: file censored %s!\n") % inst)
276 except error.RevlogError as inst:
277 ui.warn(_("abort: %s!\n") % inst)
278 except error.SignalInterrupt:
279 ui.warn(_("killed!\n"))
280 except error.UnknownCommand as inst:
240 except error.UnknownCommand as inst:
281 ui.warn(_("hg: unknown command '%s'\n") % inst.args[0])
241 ui.warn(_("hg: unknown command '%s'\n") % inst.args[0])
282 try:
242 try:
@@ -292,61 +252,11 b' def callcatch(ui, func):'
292 suggested = True
252 suggested = True
293 if not suggested:
253 if not suggested:
294 commands.help_(ui, 'shortlist')
254 commands.help_(ui, 'shortlist')
295 except error.InterventionRequired as inst:
255 except IOError:
296 ui.warn("%s\n" % inst)
256 raise
297 if inst.hint:
298 ui.warn(_("(%s)\n") % inst.hint)
299 return 1
300 except error.Abort as inst:
301 ui.warn(_("abort: %s\n") % inst)
302 if inst.hint:
303 ui.warn(_("(%s)\n") % inst.hint)
304 except ImportError as inst:
305 ui.warn(_("abort: %s!\n") % inst)
306 m = str(inst).split()[-1]
307 if m in "mpatch bdiff".split():
308 ui.warn(_("(did you forget to compile extensions?)\n"))
309 elif m in "zlib".split():
310 ui.warn(_("(is your Python install correct?)\n"))
311 except IOError as inst:
312 if util.safehasattr(inst, "code"):
313 ui.warn(_("abort: %s\n") % inst)
314 elif util.safehasattr(inst, "reason"):
315 try: # usually it is in the form (errno, strerror)
316 reason = inst.reason.args[1]
317 except (AttributeError, IndexError):
318 # it might be anything, for example a string
319 reason = inst.reason
320 if isinstance(reason, unicode):
321 # SSLError of Python 2.7.9 contains a unicode
322 reason = reason.encode(encoding.encoding, 'replace')
323 ui.warn(_("abort: error: %s\n") % reason)
324 elif (util.safehasattr(inst, "args")
325 and inst.args and inst.args[0] == errno.EPIPE):
326 pass
327 elif getattr(inst, "strerror", None):
328 if getattr(inst, "filename", None):
329 ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
330 else:
331 ui.warn(_("abort: %s\n") % inst.strerror)
332 else:
333 raise
334 except OSError as inst:
335 if getattr(inst, "filename", None) is not None:
336 ui.warn(_("abort: %s: '%s'\n") % (inst.strerror, inst.filename))
337 else:
338 ui.warn(_("abort: %s\n") % inst.strerror)
339 except KeyboardInterrupt:
257 except KeyboardInterrupt:
340 raise
258 raise
341 except MemoryError:
259 except: # probably re-raises
342 ui.warn(_("abort: out of memory\n"))
343 except SystemExit as inst:
344 # Commands shouldn't sys.exit directly, but give a return code.
345 # Just in case catch this and and pass exit code to caller.
346 return inst.code
347 except socket.error as inst:
348 ui.warn(_("abort: %s\n") % inst.args[-1])
349 except: # perhaps re-raises
350 if not handlecommandexception(ui):
260 if not handlecommandexception(ui):
351 raise
261 raise
352
262
@@ -14,6 +14,7 b' import hashlib'
14 import os
14 import os
15 import re
15 import re
16 import shutil
16 import shutil
17 import socket
17 import stat
18 import stat
18 import tempfile
19 import tempfile
19 import threading
20 import threading
@@ -141,6 +142,108 b' def nochangesfound(ui, repo, excluded=No'
141 else:
142 else:
142 ui.status(_("no changes found\n"))
143 ui.status(_("no changes found\n"))
143
144
145 def callcatch(ui, func):
146 """call func() with global exception handling
147
148 return func() if no exception happens. otherwise do some error handling
149 and return an exit code accordingly. does not handle all exceptions.
150 """
151 try:
152 return func()
153 # Global exception handling, alphabetically
154 # Mercurial-specific first, followed by built-in and library exceptions
155 except error.LockHeld as inst:
156 if inst.errno == errno.ETIMEDOUT:
157 reason = _('timed out waiting for lock held by %s') % inst.locker
158 else:
159 reason = _('lock held by %s') % inst.locker
160 ui.warn(_("abort: %s: %s\n") % (inst.desc or inst.filename, reason))
161 except error.LockUnavailable as inst:
162 ui.warn(_("abort: could not lock %s: %s\n") %
163 (inst.desc or inst.filename, inst.strerror))
164 except error.OutOfBandError as inst:
165 if inst.args:
166 msg = _("abort: remote error:\n")
167 else:
168 msg = _("abort: remote error\n")
169 ui.warn(msg)
170 if inst.args:
171 ui.warn(''.join(inst.args))
172 if inst.hint:
173 ui.warn('(%s)\n' % inst.hint)
174 except error.RepoError as inst:
175 ui.warn(_("abort: %s!\n") % inst)
176 if inst.hint:
177 ui.warn(_("(%s)\n") % inst.hint)
178 except error.ResponseError as inst:
179 ui.warn(_("abort: %s") % inst.args[0])
180 if not isinstance(inst.args[1], basestring):
181 ui.warn(" %r\n" % (inst.args[1],))
182 elif not inst.args[1]:
183 ui.warn(_(" empty string\n"))
184 else:
185 ui.warn("\n%r\n" % util.ellipsis(inst.args[1]))
186 except error.CensoredNodeError as inst:
187 ui.warn(_("abort: file censored %s!\n") % inst)
188 except error.RevlogError as inst:
189 ui.warn(_("abort: %s!\n") % inst)
190 except error.SignalInterrupt:
191 ui.warn(_("killed!\n"))
192 except error.InterventionRequired as inst:
193 ui.warn("%s\n" % inst)
194 if inst.hint:
195 ui.warn(_("(%s)\n") % inst.hint)
196 return 1
197 except error.Abort as inst:
198 ui.warn(_("abort: %s\n") % inst)
199 if inst.hint:
200 ui.warn(_("(%s)\n") % inst.hint)
201 except ImportError as inst:
202 ui.warn(_("abort: %s!\n") % inst)
203 m = str(inst).split()[-1]
204 if m in "mpatch bdiff".split():
205 ui.warn(_("(did you forget to compile extensions?)\n"))
206 elif m in "zlib".split():
207 ui.warn(_("(is your Python install correct?)\n"))
208 except IOError as inst:
209 if util.safehasattr(inst, "code"):
210 ui.warn(_("abort: %s\n") % inst)
211 elif util.safehasattr(inst, "reason"):
212 try: # usually it is in the form (errno, strerror)
213 reason = inst.reason.args[1]
214 except (AttributeError, IndexError):
215 # it might be anything, for example a string
216 reason = inst.reason
217 if isinstance(reason, unicode):
218 # SSLError of Python 2.7.9 contains a unicode
219 reason = reason.encode(encoding.encoding, 'replace')
220 ui.warn(_("abort: error: %s\n") % reason)
221 elif (util.safehasattr(inst, "args")
222 and inst.args and inst.args[0] == errno.EPIPE):
223 pass
224 elif getattr(inst, "strerror", None):
225 if getattr(inst, "filename", None):
226 ui.warn(_("abort: %s: %s\n") % (inst.strerror, inst.filename))
227 else:
228 ui.warn(_("abort: %s\n") % inst.strerror)
229 else:
230 raise
231 except OSError as inst:
232 if getattr(inst, "filename", None) is not None:
233 ui.warn(_("abort: %s: '%s'\n") % (inst.strerror, inst.filename))
234 else:
235 ui.warn(_("abort: %s\n") % inst.strerror)
236 except MemoryError:
237 ui.warn(_("abort: out of memory\n"))
238 except SystemExit as inst:
239 # Commands shouldn't sys.exit directly, but give a return code.
240 # Just in case catch this and and pass exit code to caller.
241 return inst.code
242 except socket.error as inst:
243 ui.warn(_("abort: %s\n") % inst.args[-1])
244
245 return -1
246
144 def checknewlabel(repo, lbl, kind):
247 def checknewlabel(repo, lbl, kind):
145 # Do not use the "kind" parameter in ui output.
248 # Do not use the "kind" parameter in ui output.
146 # It makes strings difficult to translate.
249 # It makes strings difficult to translate.
@@ -92,6 +92,7 b''
92 */mercurial/dispatch.py:* in dispatch (glob)
92 */mercurial/dispatch.py:* in dispatch (glob)
93 */mercurial/dispatch.py:* in _runcatch (glob)
93 */mercurial/dispatch.py:* in _runcatch (glob)
94 */mercurial/dispatch.py:* in callcatch (glob)
94 */mercurial/dispatch.py:* in callcatch (glob)
95 */mercurial/scmutil.py* in callcatch (glob)
95 */mercurial/dispatch.py:* in _runcatchfunc (glob)
96 */mercurial/dispatch.py:* in _runcatchfunc (glob)
96 */mercurial/dispatch.py:* in _dispatch (glob)
97 */mercurial/dispatch.py:* in _dispatch (glob)
97 */mercurial/dispatch.py:* in runcommand (glob)
98 */mercurial/dispatch.py:* in runcommand (glob)
@@ -127,6 +128,7 b''
127 */mercurial/dispatch.py:* in dispatch (glob)
128 */mercurial/dispatch.py:* in dispatch (glob)
128 */mercurial/dispatch.py:* in _runcatch (glob)
129 */mercurial/dispatch.py:* in _runcatch (glob)
129 */mercurial/dispatch.py:* in callcatch (glob)
130 */mercurial/dispatch.py:* in callcatch (glob)
131 */mercurial/scmutil.py* in callcatch (glob)
130 */mercurial/dispatch.py:* in _runcatchfunc (glob)
132 */mercurial/dispatch.py:* in _runcatchfunc (glob)
131 */mercurial/dispatch.py:* in _dispatch (glob)
133 */mercurial/dispatch.py:* in _dispatch (glob)
132 */mercurial/dispatch.py:* in runcommand (glob)
134 */mercurial/dispatch.py:* in runcommand (glob)
@@ -150,6 +152,7 b''
150 */mercurial/dispatch.py:* in dispatch (glob)
152 */mercurial/dispatch.py:* in dispatch (glob)
151 */mercurial/dispatch.py:* in _runcatch (glob)
153 */mercurial/dispatch.py:* in _runcatch (glob)
152 */mercurial/dispatch.py:* in callcatch (glob)
154 */mercurial/dispatch.py:* in callcatch (glob)
155 */mercurial/scmutil.py* in callcatch (glob)
153 */mercurial/dispatch.py:* in _runcatchfunc (glob)
156 */mercurial/dispatch.py:* in _runcatchfunc (glob)
154 */mercurial/dispatch.py:* in _dispatch (glob)
157 */mercurial/dispatch.py:* in _dispatch (glob)
155 */mercurial/dispatch.py:* in runcommand (glob)
158 */mercurial/dispatch.py:* in runcommand (glob)
General Comments 0
You need to be logged in to leave comments. Login now