[Twisted-Python] Passing additional arguments to errback

Maciej Wasilak wasilak at gmail.com
Thu Sep 5 01:24:06 MDT 2013


Laurens,

> Cześć

Cześć! :)

Thank you for your answer. I'm explicitly interested in the following
combination:

> .addCallback(cb).addErrback(eb)

If I understand correctly errback "eb" catches errors from both callback
"cb", and from agent.request (agent.request errors pass through default
empty errback that re-raises them).

> Also, if you want to do scraping with Twisted, consider looking at
Scrapy, a fully-featured web scraper that uses Twisted internally.

I'm working on CoAP protocol which runs on top of UDP. My actual code is
request processing (but the problem is the same as in my previous post):

_________________________________________________
def processRequest(self, request):
        (...)
        d = defer.succeed(request)
        d.addCallback(self.processBlock1)
        d.addCallback(self.dispatchRequest)
def dispatchRequest(self, request):
        (...)
        resource = self.endpoint.getResourceFor(request)
        if resource is None:
            response = Message(code=NOT_FOUND, payload='Resource not found!'
)
            self.respond(response, request)
            return
        try:
            d = resource.render(request)
        except iot.error.UnallowedMethod:
            response = Message(code=METHOD_NOT_ALLOWED, payload='Method not
allowed!')
            self.respond(response, request)
            return
        except iot.error.UnsupportedMethod:
            response = Message(code=NOT_IMPLEMENTED, payload='Method not
implemented!')
            self.respond(response, request)
            return
        delayed_ack = reactor.callLater(EMPTY_ACK_DELAY, self.sendEmptyAck,
request)
        d.addCallback(self.respond, request, delayed_ack)
        return d

__________________________________________________

I would like to rewrite it to:

__________________________________________________
def processRequest(self, request):
        (...)
        d = defer.succeed(request)
        d.addCallback(self.processBlock1)
        d.addCallback(self.dispatchRequest
).addErrback(self.handleRequestError)

def dispatchRequest(self, request):
        (...)
        resource = self.endpoint.getResourceFor(request)
        if resource is None:
            raise NoResource()
        try:
            d = resource.render(request)
        except iot.error.UnallowedMethod:      #Explicit re-rise for this
example only
            raise UnallowedMethod()
        except iot.error.UnsupportedMethod:
            raise UnsupportedMethod()
        d.addCallback(self.respond, request)
        return d


def handleRequestErrors(self, failure, request???)
        # handle exceptions, send error response to client

_______________________________________________

I would like to handle Exceptions in handleRequestErrors(). However
handleRequestErrors() has to send a response to the client, so it needs the
request, and I don't know how to pass it from inside dispatchRequest().

I see two possibilities:
    1. Pass request inside Failure object
    2. Leave the original code (process errors inside callback)

Which solution is more elegant?

Best Regards

Maciek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20130905/e3d386c4/attachment-0001.html>


More information about the Twisted-Python mailing list