问题
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