Example #1
0
 def processFinalization(self, final, response, responseCnt, **keyargs):
     '''
     Process the finalization.
     '''
     assert isinstance(response, Response), 'Invalid response %s' % response
     assert isinstance(
         responseCnt,
         ResponseContent), 'Invalid response content %s' % responseCnt
     if isinstance(responseCnt.source, Iterable):
         content = BytesIO()
         try:
             for bytes in responseCnt.source:
                 content.write(bytes)
         except:
             log.exception(
                 'Exception occurred while processing the content')
             error = StringIO()
             traceback.print_exc(file=error)
             INTERNAL_ERROR.set(response)
             response.headers = dict(self.errorHeaders)
             responseCnt.source = convertToBytes(self.errorResponse(error),
                                                 'UTF-8',
                                                 'backslashreplace')
         else:
             content.seek(0)
             responseCnt.source = content
Example #2
0
    def processError(self, error, response, responseCnt, **keyargs):
        '''
        Process the error.
        '''
        assert isinstance(error, Error), 'Invalid error execution %s' % error
        assert isinstance(response, Response), 'Invalid response %s' % response
        assert isinstance(
            responseCnt,
            ResponseContent), 'Invalid response content %s' % responseCnt
        assert isinstance(
            error.exception,
            Exception), 'Invalid error exception %s' % error.exception

        if error.isRetrying: return  # Maybe next time
        error.suppress()

        excInfo = (type(error.exception), error.exception,
                   error.exception.__traceback__)
        log.error('Exception occurred while processing the execution',
                  exc_info=excInfo)

        # If there is an explanation for the error occurred, we do not need to make another one
        if responseCnt.source is not None: return

        ferror = StringIO()
        traceback.print_exception(*excInfo, file=ferror)
        INTERNAL_ERROR.set(response)
        response.headers = dict(self.errorHeaders)
        responseCnt.source = convertToBytes(self.errorResponse(ferror),
                                            'UTF-8', 'backslashreplace')
Example #3
0
 def handleError(self, chain, response, responseCnt):
     '''
     Handle the error.
     '''
     assert isinstance(chain, Chain), 'Invalid processors chain %s' % chain
     assert isinstance(response, Response), 'Invalid response %s' % response
     assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt
     
     # If there is an explanation for the error occurred, we do not need to make another one
     if ResponseContent.source in responseCnt: return
     
     log.exception('Exception occurred while processing the chain')
     error = StringIO()
     traceback.print_exc(file=error)
     response.code, response.isSuccess = INTERNAL_ERROR
     response.text = 'Upps, please consult the server logs'
     response.headers = self.errorHeaders
     responseCnt.source = convertToBytes(self.errorResponse(error), 'utf8', 'backslashreplace')
Example #4
0
    def process(self, chain, response:Response, responseCnt:ResponseContent, **keyargs):
        '''
        @see: HandlerProcessor.process
        
        Provides the additional arguments by type to be populated.
        '''
        assert isinstance(chain, Chain), 'Invalid processors chain %s' % chain
        assert isinstance(response, Response), 'Invalid response %s' % response
        assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt

        error = None
        try:
            try:
                chain.process(response=response, responseCnt=responseCnt, **keyargs)
                # We process the chain internally so we might cache any exception.
            except DevelError as e:
                response.code, response.text, response.errorMessage = BAD_CONTENT, 'Development error', str(e)
                chain.process(response=response, responseCnt=responseCnt, **keyargs)
                # We try to process now the chain (where it left of) with the exception set.
        except:
            log.exception('Exception occurred while processing the chain')
            error = StringIO()
            traceback.print_exc(file=error)
        else:
            if __debug__ and isinstance(responseCnt.source, Iterable):
                # If in debug mode and the response content has a source generator then we will try to read that
                # in order to catch any exception before the actual streaming.
                content = BytesIO()
                try:
                    for bytes in responseCnt.source: content.write(bytes)
                except:
                    log.exception('Exception occurred while processing the chain')
                    error = StringIO()
                    traceback.print_exc(file=error)
                else:
                    content.seek(0)
                    responseCnt.source = readGenerator(content)

        if error is not None:
            response.code = INTERNAL_ERROR
            response.text = 'Upps, please consult the server logs'
            response.headers = self.errorHeaders
            responseCnt.source = convertToBytes(self.errorResponse(error), 'utf8', 'backslashreplace')
Example #5
0
 def onFinalize():
     '''
     Handle the finalization
     '''
     if isinstance(responseCnt.source, Iterable):
         content = BytesIO()
         try:
             for bytes in responseCnt.source: content.write(bytes)
         except:
             log.exception('Exception occurred while processing the chain')
             error = StringIO()
             traceback.print_exc(file=error)
             response.code, response.isSuccess = INTERNAL_ERROR
             response.text = 'Cannot render response, please consult the server logs'
             response.headers = self.errorHeaders
             responseCnt.source = convertToBytes(self.errorResponse(error), 'utf8', 'backslashreplace')
         else:
             content.seek(0)
             responseCnt.source = content
Example #6
0
 def processFinalization(self, final, response, responseCnt, **keyargs):
     '''
     Process the finalization.
     '''
     assert isinstance(response, Response), 'Invalid response %s' % response
     assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt
     if isinstance(responseCnt.source, Iterable):
         content = BytesIO()
         try:
             for bytes in responseCnt.source: content.write(bytes)
         except:
             log.exception('Exception occurred while processing the content')
             error = StringIO()
             traceback.print_exc(file=error)
             INTERNAL_ERROR.set(response)
             response.headers = dict(self.errorHeaders)
             responseCnt.source = convertToBytes(self.errorResponse(error), 'UTF-8', 'backslashreplace')
         else:
             content.seek(0)
             responseCnt.source = content
Example #7
0
 def onFinalize():
     '''
     Handle the finalization
     '''
     try: response, responseCnt = chain.arg.response, chain.arg.responseCnt
     except AttributeError: return  # If there is no response or response content we take no action
     assert isinstance(response, Response), 'Invalid response %s' % response
     assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt
     if isinstance(responseCnt.source, Iterable):
         content = BytesIO()
         try:
             for bytes in responseCnt.source: content.write(bytes)
         except:
             log.exception('Exception occurred while processing the chain')
             error = StringIO()
             traceback.print_exc(file=error)
             response.code, response.status, response.isSuccess = INTERNAL_ERROR
             response.headers = self.errorHeaders
             responseCnt.source = convertToBytes(self.errorResponse(error), 'utf8', 'backslashreplace')
         else:
             content.seek(0)
             responseCnt.source = content
Example #8
0
 def processError(self, error, response, responseCnt, **keyargs):
     '''
     Process the error.
     '''
     assert isinstance(error, Error), 'Invalid error execution %s' % error
     assert isinstance(response, Response), 'Invalid response %s' % response
     assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt
     assert isinstance(error.exception, Exception), 'Invalid error exception %s' % error.exception
     
     if error.isRetrying: return  # Maybe next time
     error.suppress()
     
     excInfo = (type(error.exception), error.exception, error.exception.__traceback__)
     log.error('Exception occurred while processing the execution', exc_info=excInfo)
     
     # If there is an explanation for the error occurred, we do not need to make another one
     if responseCnt.source is not None: return
     
     ferror = StringIO()
     traceback.print_exception(*excInfo, file=ferror)
     INTERNAL_ERROR.set(response)
     response.headers = dict(self.errorHeaders)
     responseCnt.source = convertToBytes(self.errorResponse(ferror), 'UTF-8', 'backslashreplace')
Example #9
0
 def handleError(self, chain):
     '''
     Handle the error.
     '''
     assert isinstance(chain, Chain), 'Invalid processors chain %s' % chain
     try: response = chain.arg.response
     except AttributeError: response = Response()
     
     try: responseCnt = chain.arg.responseCnt
     except AttributeError: responseCnt = ResponseContent()
     
     assert isinstance(response, Response), 'Invalid response %s' % response
     assert isinstance(responseCnt, ResponseContent), 'Invalid response content %s' % responseCnt
     
     # If there is an explanation for the error occurred, we do not need to make another one
     if responseCnt.source is not None: return
     
     log.exception('Exception occurred while processing the chain')
     error = StringIO()
     traceback.print_exc(file=error)
     response.code, response.status, response.isSuccess = INTERNAL_ERROR
     response.headers = self.errorHeaders
     responseCnt.source = convertToBytes(self.errorResponse(error), 'utf-8', 'backslashreplace')