Beispiel #1
0
def delegate_error_to_simplate(website,
                               state,
                               response,
                               request=None,
                               resource=None):
    if request is None:
        return  # early parsing must've failed
    if response.code < 400:
        return

    code = str(response.code)
    possibles = [code + ".spt", "error.spt"]
    fspath = _first(website.ours_or_theirs(errpage) for errpage in possibles)

    if fspath is not None:
        request.original_resource = resource
        resource = resources.get(website.request_processor, fspath)
        state['dispatch_result'] = DispatchResult(DispatchStatus.okay, fspath,
                                                  {}, 'Found.', {}, True)
        # Try to return an error that matches the type of the response the
        # client would have received if the error didn't occur
        wanted = getattr(state.get('output'), 'media_type', None) or ''
        # If we don't have a media type (e.g. when we're returning a 404), then
        # we fall back to the Accept header
        wanted += ',' + (state.get('accept_header') or '')
        # As a last resort we accept anything, with a preference for text/plain
        wanted += ',text/plain;q=0.2,*/*;q=0.1'
        state['accept_header'] = wanted.lstrip(',')

        render_response(state, resource, response, website.request_processor)
Beispiel #2
0
def delegate_error_to_simplate(website, state, response, request=None, resource=None):
    if request is None:
        return  # early parsing must've failed
    if response.code < 400:
        return

    code = str(response.code)
    possibles = [code + ".spt", "error.spt"]
    fspath = _first(website.ours_or_theirs(errpage) for errpage in possibles)

    if fspath is not None:
        request.original_resource = resource
        resource = resources.get(website.request_processor, fspath)
        state['dispatch_result'] = DispatchResult( DispatchStatus.okay
                                                 , fspath
                                                 , {}
                                                 , 'Found.'
                                                 , {}
                                                 , True
                                                  )
        # Try to return an error that matches the type of the response the
        # client would have received if the error didn't occur
        wanted = getattr(state.get('output'), 'media_type', None) or ''
        # If we don't have a media type (e.g. when we're returning a 404), then
        # we fall back to the Accept header
        wanted += ',' + (state.get('accept_header') or '')
        # As a last resort we accept anything, with a preference for text/plain
        wanted += ',text/plain;q=0.2,*/*;q=0.1'
        state['accept_header'] = wanted.lstrip(',')

        render_response(state, resource, response, website.request_processor)
Beispiel #3
0
def delegate_error_to_simplate(website, request, response):
    if response.code < 400:
        return

    code = str(response.code)
    possibles = [code + ".html", code + ".html.spt", "error.html", "error.html.spt"]
    fs = _first(website.ours_or_theirs(errpage) for errpage in possibles)

    if fs is not None:
        request.fs = fs
        request.original_resource = request.resource
        resource = resources.get(request)
        response = resource.respond(request, response)

    return {'response': response, 'exception': None}
Beispiel #4
0
def delegate_error_to_simplate(website, request, response, resource=None):
    if response.code < 400:
        return

    code = str(response.code)
    possibles = [code + ".spt", "error.spt"]
    fs = _first(website.ours_or_theirs(errpage) for errpage in possibles)

    if fs is not None:
        request.fs = fs
        request.original_resource = resource
        if resource is not None:
            # Try to return an error that matches the type of the original resource.
            request.headers['Accept'] = resource.media_type + ', text/plain; q=0.1'
        resource = resources.get(request)
        try:
            response = resource.respond(request, response)
        except Response as response:
            if response.code != 406:
                raise

    return {'response': response, 'exception': None}
Beispiel #5
0
def delegate_error_to_simplate(website, state, response, request=None, resource=None):
    if request is None:
        return  # early parsing must've failed
    if response.code < 400:
        return

    code = str(response.code)
    possibles = [code + ".spt", "error.spt"]
    fspath = _first(website.ours_or_theirs(errpage) for errpage in possibles)

    if fspath is not None:
        request.original_resource = resource
        if resource is not None and resource.default_media_type != website.media_type_default:
            # Try to return an error that matches the type of the original resource.
            state["accept_header"] = resource.default_media_type + ", text/plain; q=0.1"
        resource = resources.get(website, fspath)
        state["dispatch_result"] = DispatchResult(DispatchStatus.okay, fspath, {}, "Found.", {}, True)
        try:
            response = resource.respond(state)
        except Response as response:
            if response.code != 406:
                raise

    return {"response": response, "exception": None}
Beispiel #6
0
def delegate_error_to_simplate(website, response, request=None, resource=None):
    if request is None:
        return  # early parsing must've failed
    if response.code < 400:
        return

    code = str(response.code)
    possibles = [code + ".spt", "error.spt"]
    fspath = _first(website.ours_or_theirs(errpage) for errpage in possibles)

    if fspath is not None:
        request.original_resource = resource
        if resource is not None:
            # Try to return an error that matches the type of the original resource.
            request.headers['Accept'] = resource.media_type + ', text/plain; q=0.1'
        resource = resources.get(website, fspath)
        dispatch_result = DispatchResult(DispatchStatus.okay, fspath, {}, 'Found.', {}, True)
        try:
            response = resource.respond(request, dispatch_result, response)
        except Response as response:
            if response.code != 406:
                raise

    return {'response': response, 'exception': None}