Twisted: how to get error parameters from failure?

天涯浪子 提交于 2020-01-04 05:55:36

问题


I have a piece of code:

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()

Is the way presented in newEb the only way to extract the error parameters from failure? When I use failure.trap or failure.check I cannot receive error instance.


回答1:


If by error parameters you mean the exception instances, then Failure.value.



来源:https://stackoverflow.com/questions/8181990/twisted-how-to-get-error-parameters-from-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!