Python werkzeug.exceptions.default_exceptions() Examples

The following are 4 code examples of werkzeug.exceptions.default_exceptions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module werkzeug.exceptions , or try the search function .
Example #1
Source File: server.py    From octavia with Apache License 2.0 5 votes vote down vote up
def register_app_error_handler(app):
    for code in exceptions.default_exceptions:
        app.register_error_handler(code, make_json_error) 
Example #2
Source File: test_error_handler.py    From flask-smorest with MIT License 5 votes vote down vote up
def test_error_handler_on_abort(self, app, code):

        client = app.test_client()

        @app.route('/abort')
        def test_abort():
            abort(code)

        Api(app)

        response = client.get('/abort')
        assert response.status_code == code
        assert response.json['code'] == code
        assert response.json['status'] == default_exceptions[code]().name 
Example #3
Source File: exceptions.py    From flask-resty with MIT License 5 votes vote down vote up
def get_default_errors(cls, status_code):
        if status_code not in default_exceptions:
            return ()

        exc = default_exceptions[status_code]()
        return (cls.get_error_from_http_exception(exc),) 
Example #4
Source File: utils.py    From kuryr-libnetwork with Apache License 2.0 5 votes vote down vote up
def make_json_app(import_name, **kwargs):
    """Creates a JSON-oriented Flask app.

    All error responses that you don't specifically manage yourself will have
    application/json content type, and will contain JSON that follows the
    libnetwork remote driver protocol.


    { "Err": "405: Method Not Allowed" }


    See:
      - https://github.com/docker/libnetwork/blob/3c8e06bc0580a2a1b2440fe0792fbfcd43a9feca/docs/remote.md#errors  # noqa
    """
    app = flask.Flask(import_name, **kwargs)

    @app.errorhandler(exceptions.KuryrException)
    @app.errorhandler(n_exceptions.NeutronClientException)
    @app.errorhandler(jsonschema.ValidationError)
    @app.errorhandler(processutils.ProcessExecutionError)
    def make_json_error(ex):
        LOG.error("Unexpected error happened: %s", ex)
        traceback.print_exc(file=sys.stderr)
        response = flask.jsonify({"Err": str(ex)})
        response.status_code = w_exceptions.InternalServerError.code
        if isinstance(ex, w_exceptions.HTTPException):
            response.status_code = ex.code
        elif isinstance(ex, n_exceptions.NeutronClientException):
            response.status_code = ex.status_code
        elif isinstance(ex, jsonschema.ValidationError):
            response.status_code = w_exceptions.BadRequest.code
        content_type = 'application/vnd.docker.plugins.v1+json; charset=utf-8'
        response.headers['Content-Type'] = content_type
        return response

    for code in w_exceptions.default_exceptions:
        app.register_error_handler(code, make_json_error)

    return app