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
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python