Example #1
0
 def _parse_error(self, message):
     """Raise a parse error."""
     error = ParseError(message)
     error.filename = self.m_frames[-1].filename
     error.lineno = self.m_frames[-1].lineno
     error.backtrace = get_backtrace()
     raise error
Example #2
0
 def _import(self, modname):
     """Import a module `modname'."""
     try:
         mod = __import__(modname, globals(), locals())
     except SyntaxError, err:
         error = DracoSiteError("Syntax error in module.")
         error.filename = modname
         error.lineno = err.lineno
         error.backtrace = get_backtrace()
         raise error
Example #3
0
def handle_request(iface):
    """Handle a request."""
    # Acquire the read-write lock. This lock is held by all threads in
    # read mode when serving a Draco request. If one of the global Draco
    # objects needs to be reloaded, the lock is upgraded to write mode
    # by the thread that detected the reload condition. All threads will
    # then serialize and the reload can continue.
    rwlock.acquire_read()
    try:
        # Once off initialization. No user code can be run from this, and
        # there's no option of recording errors.
        initlock.acquire()
        try:
            global initialized, options
            if not initialized:
                options = init_options(iface.options())
                init_logger(options)
                init_codecs(options)
                initialized = True
        finally:
            initlock.release()

        # Dispatch the request. Errors are handled from now on.
        debug = options.get('debug')
        profile = options.get('profile')
        logger = logging.getLogger('draco2.core.dispatch')
        try:
            agent = iface.headers_in().get('user-agent', [''])[0]
            logger.debug('Request: %s (%s)' % (iface.uri(), agent))
            t1 = time.time()
            if profile:
                import lsprof
                stats = lsprof.profile(dispatch_request, iface)
                stats.sort('inlinetime')
                io = StringIO()
                stats.pprint(top=20, file=io)
                logger.debug(io.getvalue())
                stats.sort('totaltime')
                io = StringIO()
                stats.pprint(top=20, file=io)
                logger.debug(io.getvalue())
            else:
                dispatch_request(iface)
            t2 = time.time()
            logger.debug('Total time spent: %.2f (%s)' % ((t2 - t1), iface.uri()))
            return

        except HTTPResponse, exc:
            status = exc.status
            headers = exc.headers
            message = http.http_reason_strings[status]
            if not hasattr(exc, 'backtrace') or not exc.backtrace:
                exc.backtrace = get_backtrace()
            errorname = 'error_response_%03d' % status
            exception = exc

        except (StandardError, DracoError), exc:
            status = http.HTTP_INTERNAL_SERVER_ERROR
            headers = {}
            if not hasattr(exc, 'backtrace') or not exc.backtrace:
                exc.backtrace = get_backtrace()
            if debug:
                message = exc.backtrace
            else:
                message = http.http_reason_strings[status]
            errorname = 'uncaught_exception'
            exception = exc
            logger.error('A uncaught exception occurred. Backtrace follows.')
            logger.error(exc.backtrace)
Example #4
0
        iface.internal_redirect(uri)
        iface.set_error(exception)

        try:
            dispatch_request(iface)
            return

        except HTTPResponse, exc:
            if exc.status != http.HTTP_NOT_FOUND:
                logger.error('HTTP response %s in error handler.' % exc.status)

        except (StandardError, DracoError), exc:
            # Log the error but display the original one to the user.
            logger.error('Uncaught exception in error handler. Backtrace follows.')
            if not hasattr(exc, 'backtrace') or not exc.backtrace:
                exc.backtrace = get_backtrace()
            logger.error(exc.backtrace)

        if iface.header_sent():
            logger.error('Header was sent by error handler, cannot continue.')
            return

        # Finally, do a simple error response.
        iface.simple_response(status, headers, message)

    finally:
        rwlock.release_read()


def dispatch_request(iface):
    """Dispatch a request to the proper handler."""