Vasiliy
Vasiliy

Reputation: 55

Twisted: how to get error parameters from failure?

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.

Upvotes: 2

Views: 1385

Answers (1)

Jean-Paul Calderone
Jean-Paul Calderone

Reputation: 48335

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

Upvotes: 1

Related Questions