Process the body of 403 Forbidden with twisted.web.client

Question:

I have the general code:

client.getPage(url, headers=headers).addCallback(...).addErrback(errorHandler)

with the following error handler:

def errorHandler(self, error):
    log.critical('Error: ', extra=dict(error=error))

All that I can get from such processing is the following:

Error: {'error': <twisted.python.failure.Failure twisted.web.error.Error: 403 Forbidden>}

Problem is that I need to process the body of the error response (yep, that’s AWS), but there’s no API in twisted.web.error.Error to do it, and I couldn’t find any examples on the Internet.

General example contains this:

def errorHandler(self, error):
    # release memory and handlers 
    print(error)

Does anyone know how to do it?

Asked By: Leontyev Georgiy

||

Answers:

Looking at the Twisted docs, I think you should be able to do this:

def errorHandler(self, error):
    # `error` is a twisted.python.failure.Failure
    original_exc = error.value
    # `original_exc` is a twisted.web.error.Error
    response_body = original_exc.response
    log.critical('Error: %s', response.body, extra=dict(error=error))
Answered By: Anentropic
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.