Twisted:如何从失败中获取错误参数?

时间:2011-11-18 11:53:57

标签: python twisted deferred custom-errors

我有一段代码:

from twisted.web.client import getPage
from twisted.internet import reactor

class TestError(Exception):
    def __init__(self, message):
        self.message = message
    def __repr__(self):
        return 'TestError'

def gotPage(response):
    print response
    reactor.stop()

def gotErr(failure):
    raise TestError('This is error')

def newEb(failure):
    try:
        failure.raiseException()
    except TestError as te:
        print te.message
    reactor.stop()

if __name__ == '__main__':
    deferred = getPage('http://somebadpage.net', method='GET')
    deferred.addCallback(gotPage)
    deferred.addErrback(gotErr)
    deferred.addErrback(newEb)
    reactor.run()

在newEb中呈现的方式是从失败中提取错误参数的唯一方法吗? 当我使用failure.trap或failure.check时,我无法收到错误实例。

1 个答案:

答案 0 :(得分:1)

如果通过错误参数表示异常情况,则为Failure.value