Пример #1
0
def api_client_error(context, request):
    """
    Catches all HTTP client errors (400, etc), except 404 and 403,
    which Pyramid seems to require handling separately.

    Note that the errors output from this view matches that what Cornice
    generates, which makes responses the same on the front end.
    """
    request.response.status = context.code

    return {
        "status": "error",
        "errors": [
            {"name": context.title, "location": "unknown", "description": get_http_exception_description(context)}
        ],
    }
Пример #2
0
def api_not_found(context, request):
    """
    Catches 404.

    Note that @notfound_view_config cannot be used here, because we need
    the exception object to get the description and title.

    Note that the errors output from this view matches that what Cornice
    generates, which makes responses the same on the front end.
    """
    request.response.status = context.code

    return {
        "status": "error",
        "errors": [{"name": context.title, "location": "url", "description": get_http_exception_description(context)}],
    }
Пример #3
0
def api_forbidden(context, request):
    """
    Catches 403, raises 401.

    Note that @forbidden_view_config cannot be used here, because we need
    the exception object to get the description and title.

    Note that the errors output from this view matches that what Cornice
    generates, which makes responses the same on the front end.

    Note that this @forbidden_view_config returns JSON and only gets used
    when calling the API using application/json as the Accept header.
    There is another @forbidden_view_config in auth views that renders the
    login form, but that only gets used if the Accept header is text/html.
    """
    # this actually changes the status code from 403 to 401
    request.response.status = 401

    return {
        "status": "error",
        "errors": [{"name": context.title, "location": "url", "description": get_http_exception_description(context)}],
    }