如何处理Mercurial扩展中的错误

时间:2016-08-04 09:33:08

标签: python mercurial mercurial-extension

在Mercurial扩展中处理错误的推荐方法是什么?我们在网上找不到这方面的任何文档。

以下示例将在Windows shell中打印 并将 设置为


为什么 设置为 ?我们预计它会 。在这种情况下,Python或Mercurial如何处理 ?更改为返回 会产生

我们已经看到了 protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e) { DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("~/YourXmlFilePath")); ds.Tables[0].Rows.RemoveAt(e.RowIndex); ds.WriteXml(Server.MapPath("~/YourXmlFilePath")); BindGrid(); } 的一些例子,但这输出了一个非常冗长的callstack,这不是必要的。

是一个简单的短信和正确的

使用Windows 7和Mercurial 3.4.1

1 个答案:

答案 0 :(得分:0)

@command函数中,使用ui.warn()显示错误消息,然后返回整数值以设置退出代码:

if something_is_wrong:
    ui.warn(_('Something is wrong, please correct this'))
    return 1

但是,使用raise mercurial.error.Abort()也没关系,除非您将ui.traceback设置为True(默认为False),否则不会产生回溯。提升Abort()通常会导致ui.warn('abort: ' + msg_from_exception)return -1组合(导致255退出代码):

$ cat demo.py
from mercurial import cmdutil, error

cmdtable = {}
command = cmdutil.command(cmdtable)

@command('demo1', [], '')
def demo1(ui, repo, *args, **opts):
    ui.warn('Error message written directly to ui.warn')
    return 1

@command('demo2', [], '')
def demo2(ui, repo, *args, **opts):
    raise error.Abort('Error message raised with Abort')

$ hg --config extensions.demo=`pwd`/demo.py demo1
Error message written directly to ui.warn
$ echo $?
1
$ hg --config extensions.demo=`pwd`/demo.py demo2
abort: Error message raised with Abort
$?
255
$ hg --config extensions.demo=`pwd`/demo.py --config ui.traceback=true demo2
Traceback (most recent call last):
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/dispatch.py", line 204, in _runcatch
    return _dispatch(req)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/dispatch.py", line 880, in _dispatch
    cmdpats, cmdoptions)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/extensions.py", line 210, in closure
    return func(*(args + a), **kw)
  File "/opt/facebook/hg/lib/python2.7/site-packages/fastmanifest/cachemanager.py", line 318, in runcommandtrigger
    result = orig(*args, **kwargs)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/extensions.py", line 210, in closure
    return func(*(args + a), **kw)
  File "/opt/facebook/hg/lib/python2.7/site-packages/fastmanifest/__init__.py", line 174, in _logonexit
    r = orig(ui, repo, cmd, fullargs, *args)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/extensions.py", line 210, in closure
    return func(*(args + a), **kw)
  File "/opt/facebook/hg/lib/python2.7/site-packages/hgext/journal.py", line 79, in runcommand
    return orig(lui, repo, cmd, fullargs, *args)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/dispatch.py", line 637, in runcommand
    ret = _runcommand(ui, options, cmd, d)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/extensions.py", line 210, in closure
    return func(*(args + a), **kw)
  File "/opt/facebook/hg/lib/python2.7/site-packages/hgext/pager.py", line 160, in pagecmd
    return orig(ui, options, cmd, cmdfunc)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/extensions.py", line 210, in closure
    return func(*(args + a), **kw)
  File "/opt/facebook/hg/lib/python2.7/site-packages/hgext/color.py", line 503, in colorcmd
    return orig(ui_, opts, cmd, cmdfunc)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/dispatch.py", line 1010, in _runcommand
    return checkargs()
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/dispatch.py", line 971, in checkargs
    return cmdfunc()
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/dispatch.py", line 877, in <lambda>
    d = lambda: util.checksignature(func)(ui, *args, **cmdoptions)
  File "/opt/facebook/hg/lib/python2.7/site-packages/mercurial/util.py", line 1036, in check
    return func(*args, **kwargs)
  File "/tmp/demo/demo.py", line 13, in demo2
    raise error.Abort('Error message raised with Abort')
Abort: Error message raised with Abort
abort: Error message raised with Abort

请注意,我必须明确启用回溯!

在Python中,bool类型是int的子类,True的值为1False的整数值为{ {1}}。因为Mercurial希望0函数返回一个整数来设置退出代码(或@command代码退出代码0),所以通过返回None将退出代码设置为1

我通常会查看True模块和各种mercurial.commands扩展程序,以了解其他人如何解决特定问题。例如,hgext命令包含处理错误的示例:

hg grep

try: regexp = util.re.compile(pattern, reflags) except re.error as inst: ui.warn(_("grep: invalid match pattern: %s\n") % inst) return 1 代码引发hg addremove

error.Abort()

我将try: sim = float(opts.get('similarity') or 100) except ValueError: raise error.Abort(_('similarity must be a number')) 用于命令错误,error.Abort()仅在您特别需要255以外的退出代码时才返回整数。