Пример #1
0
def handle_httpexception(err: HTTPException) -> Response:
    """Return JSON instead of HTML for HTTP errors."""
    # start with the correct headers and status code from the error
    response = err.get_response()

    try:
        validation_messages = err.data.get("messages", None)
    except AttributeError:
        validation_messages = None

    error_body = ServerError(response.status_code).error_body

    if validation_messages:
        error_body_with_validation_errors = toolz.thread_first(
            error_body,
            # Remove description from dict
            (toolz.dissoc, "description"),
            # Merge other fields into the dict
            lambda x: {
                **x, "hint": "Errors with query params",
                "code": err.code,
                "message": "Validation errors",
                "errors": validation_messages
            })
        response.data = json.dumps(error_body_with_validation_errors)
    else:
        response.data = json.dumps(error_body)

    response.content_type = "application/json"
    return response
Пример #2
0
def handle_exception(e: HTTPException) -> ErrResponse:
    response = e.get_response()
    response.data = json.dumps({
        "status_code": e.code,
        "message": e.description
    })
    response.content_type = "application/json"
    return response
Пример #3
0
def handle_exception(e: HTTPException):
    response = e.get_response()
    response.data = json.dumps({
        "code": e.code,
        "name": e.name,
        "description": e.description,
    })
    response.content_type = "application/json"
    return response
Пример #4
0
def _jsonify_exception(error: HTTPException) -> Response:
    response = error.get_response()
    response.data = json.dumps({
        "name": error.name,
        "description": error.description
    })
    response.content_type = "application/json"
    logger.warning(
        f"An error occured. Error code {response.status_code}, name: {error.name}"
    )
    return response
Пример #5
0
def handle_exception(e: HTTPException) -> Response:
    """Return JSON instead of HTML for HTTP errors."""
    # start with the correct headers and status code from the error
    response = e.get_response()
    # replace the body with JSON
    response.data = json.dumps({
        'code': e.code,
        'name': e.name,
        'description': e.description,
    })
    response.content_type = 'application/json'
    return response
Пример #6
0
def handle_exception(error: HTTPException) -> Response:
    """
    Return JSON instead of HTML for HTTP errors.
    """
    # Start with the initial error response
    response = error.get_response()
    # Replace the body with JSON
    response.data = json.dumps({
        "title": error.name,
        "detail": error.description,
    })
    # Update the content type
    response.content_type = "application/problem+json"
    return response
Пример #7
0
Файл: app.py Проект: yws/Kyoukai
    async def handle_httpexception(self,
                                   ctx: HTTPRequestContext,
                                   exception: HTTPException,
                                   environ: dict = None) -> Response:
        """
        Handle a HTTP Exception.

        :param ctx: The context of the request.
        :param exception: The HTTPException
        :param environ: The fake WSGI environment.
        
        :return: A :class:`werkzeug.wrappers.Response` that handles this response.
        """
        # Try and load the error handler recursively from the ctx.route.blueprint.
        bp = ctx.bp or self.root

        if environ is None:
            environ = ctx.environ

        error_handler = bp.get_errorhandler(exception)
        if not error_handler:
            # Try the root Blueprint. This may happen if the blueprint requested isn't registered
            # properly in the root, for some reason.
            error_handler = self.root.get_errorhandler(exception)
            if not error_handler:
                # Just return the Exception's get_response.
                return exception.get_response(environ=environ)

        else:
            # Try and invoke the error handler to get the Response.
            # Wrap it in the try/except, so we can handle a default one.
            try:
                result = await error_handler.invoke(ctx, args=(exception, ))
            except HTTPException as e:
                # why tho?
                logger.warning(
                    "Error handler function raised another error, using the "
                    "response from that...")
                result = e.get_response(environ)
            except Exception as e:
                logger.exception("Error in error handler!")
                result = InternalServerError(e).get_response(environ)
                # else:
                # result = wrap_response(result, self.response_class)

            return result
Пример #8
0
    def handle_http_exception(e: HTTPException):
        """

        :param e: HTTPException:

        """
        response = e.get_response()
        # replace the body with JSON
        details = {"code": e.code, "description": f"{e.name}: {e.description}"}

        response.data = simplejson.dumps(details)

        response.content_type = "application/json"
        logger.error(e.description,
                     extra={
                         **details,
                         **{
                             "ex": str(e)
                         }
                     },
                     exc_info=True)
        return response
Пример #9
0
def jsonify_exception(error: HTTPException) -> Response:
    """Render exceptions as JSON."""
    exc_resp = error.get_response()
    response: Response = jsonify(reason=error.description)
    response.status_code = exc_resp.status_code
    return response
Пример #10
0
def handle_http_exception(e: HTTPException) -> Tuple[Text, int]:
    return e.get_response()