コード例 #1
0
def register_error_handlers(app: App) -> App:
    """Adds custom handlers for exceptions to Connexion app instance."""
    # Add error handlers
    app.add_error_handler(BadRequest, handle_bad_request)
    app.add_error_handler(ExtraParameterProblem, handle_bad_request)
    app.add_error_handler(InternalServerError, handle_internal_server_error)
    app.add_error_handler(Unauthorized, handle_unauthorized)
    logger.info('Registered custom error handlers with Connexion app.')

    # Workaround for adding a custom handler for `connexion.problem` responses
    # Responses from request and paramater validators are not raised and
    # cannot be intercepted by `add_error_handler`; see here:
    # https://github.com/zalando/connexion/issues/138
    @app.app.after_request
    def _rewrite_bad_request(response: Response) -> Response:
        if (
            response.status_code == 400 and
            response.data.decode('utf-8').find('"title":') is not None and
            "detail" in response.json
        ):
            response = handle_bad_request_validation(response)
        return response

    return app
コード例 #2
0
def register_error_handlers(app: App) -> App:
    """Adds custom handlers for exceptions to Connexion app instance."""
    # Add error handlers
    app.add_error_handler(BadRequest, handle_bad_request)
    app.add_error_handler(ExtraParameterProblem, handle_bad_request)
    app.add_error_handler(Forbidden, __handle_forbidden)
    app.add_error_handler(InternalServerError, __handle_internal_server_error)
    app.add_error_handler(Unauthorized, __handle_unauthorized)
    app.add_error_handler(WorkflowNotFound, __handle_workflow_not_found)
    logger.info('Registered custom error handlers with Connexion app.')

    # Return Connexion app instance
    return app