def try_rewrite_on_error(http_response, request, environ, ticket=None):
    """
    called from main.wsgibase to rewrite the http response.
    """
    status = int(str(http_response.status).split()[0])
    if status >= 399 and THREAD_LOCAL.routes.routes_onerror:
        keys = set(('%s/%s' % (request.application, status),
                    '%s/*' % (request.application),
                    '*/%s' % (status),
                    '*/*'))
        for (key, uri) in THREAD_LOCAL.routes.routes_onerror:
            if keys in key:
                if uri == '!':
                    # do nothing!
                    return http_response, environ
                elif '?' in uri:
                    path_info, query_string = uri.split('?', 1)
                    query_string += '&'
                else:
                    path_info, query_string = uri, ''
                query_string += \
                    'code=%s&ticket=%s&requested_uri=%s&request_url=%s' % \
                    (status, ticket, urllib.quote_plus(
                        request.env.request_uri), request.url)
                if uri.startswith('http://') or uri.startswith('https://'):
                    # make up a response
                    url = path_info + '?' + query_string
                    message = 'You are being redirected <a href="%s">here</a>'
                    return HTTP(303, message % url, Location=url), environ
                elif not environ.get('__ROUTES_ONERROR__', False):
                    # wsgibase will be called recursively with
                    # the routes_onerror path.
                    environ['__ROUTES_ONERROR__'] = True # limit recursion
                    path_info = '/' + path_info.lstrip('/')
                    environ['PATH_INFO'] = path_info
                    environ['QUERY_STRING'] = query_string
                    environ['WEB2PY_STATUS_CODE'] = status
                    return None, environ
    # do nothing!
    return http_response, environ
dataset/ETHPy150Open uwdata/termite-visualizations/web2py/gluon/rewrite.py/try_rewrite_on_error/swapped operands of `in`