Exemple #1
0
def internal_error(request, exception):
    '''
    This function handles all 500 type errors before they are sent back to the
    requester. 5xx type HTTP errors should be accompanied by an error report.
    While it is not necessary or feasible (HTTP Proxies can generate errors,
    for example) to handle all exception types, handling the HTTP 500 exception
    explicitly lets the publisher control the debugging information exposed.

    Some web frameworks may be verbose about an http 500 error, providing
    detailed debugging information that may compromise the system. It is up to
    the publisher to ensure that these features are well controlled.

    If the Polar server receives a 5xx error that does not have a report, it
    will first try to decode the body of the post request using json. This
    process will fail, and the server will then store the body of the post
    request (as opposed to its error code, message and resource) and continue
    processing.

    Server Errors:

        This section documents errors that are persisted on the server and not
        sent to the client. Note that the publisher is free to modify the
        content of these messages as they please.

        InternalError:

            Thrown when no exception object is provided (the nature of the
            exception is unknown).

            Code: InternalError
            Message: An error occurred. Please contact support.
            Message: An internal server error occurred. Please check logs.
            HTTP Error Code: 500
            Required: No
    '''
    # All exceptions handled by this function are json encoded 500 errors.
    content_type = 'application/json'
    status = 500
    headers = []

    # The content is determined below.
    content = ''

    # HTTP 500 exceptions may be of any type. If the type is JsonAppError then
    # the exception has been json encoded. If it is not, then we need to
    # substitute the message with a default message.
    if isinstance(exception, JsonAppError) == True:
        content = unicode(exception).encode('utf-8', 'replace')

    # If the exception is not a JsonAppError exception, then send a generic
    # exception.
    else:
        url = request.path
        code = 'InternalError'
        message = 'An error occurred. Please contact support.'
        debug = 'An internal server error occurred. Please check logs.'
        content = encode_error(url, code, message, debug)

    response = Response(content, headers, status, content_type)
    return response.send(request._start_response)
Exemple #2
0
def not_found(request, exception):
    '''
    This function handles any uncaught HTTP 404 errors. Remember that 4xx type
    HTTP errors should be accompanied by an error report. A 404 error is a
    common error for many web frameworks; particularly those that use regex to
    route requests. Returning a proper error report makes diagnosing such
    problems easier.

    If the Polar server receives a 4xx error that does not have a report, it
    will first try to decode the body of the post request using json. This
    process will fail, and the server will then store the body of the post
    request (as opposed to its error code, message and resource) and continue
    processing.

    Errors:

        NoHandler:

            Thrown when the URL could not be routed to a handler. This error
            code should be thrown when the web framework does not understand
            the request being issued. It would imply that the API that the
            Polar server expects is not implemented on the publisher.

            Code: NoHandler
            Message: An error occurred. Please contact support.
            Debug: No handler could be found for the requested resource.
            HTTP Error Code: 404
            Required: No
    '''
    # All exceptions handled by this function are json encoded 404 errors.
    content_type = 'application/json'
    status = 404
    headers = []

    # The content is determined below.
    content = ''

    # If the exception is json encoded, we can use the content directly.
    if isinstance(exception, JsonNotFound) == True:
        content = unicode(exception).encode('utf-8', 'replace')

    # If the exception is not an AppError exception, then send a generic
    # exception, encoded as json.
    else:
        url = request.path
        code = 'NoHandler'
        message = 'An error occurred. Please contact support.'
        debug = 'No handler could be found for the requested resource.'
        content = encode_error(url, code, message, debug)

    response = Response(content, headers, status, content_type)
    return response.send(request._start_response)