2011-11-18 76 views
2

我有一段代碼:扭曲:如何從錯誤中獲取錯誤參數?

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

如果通過錯誤參數您的意思是例外情況,然後Failure.value