noirbizarre / flask-restplus

Fully featured framework for fast, easy and documented API development with Flask
http://flask-restplus.readthedocs.org
Other
2.73k stars 508 forks source link

If use Custom Scripts, Flask.ErrorHandler is not working when FlaskRestplus raise exception #750

Open MyJoiT opened 4 years ago

MyJoiT commented 4 years ago

I use Custom Scripts (provide by Flask Click) to configure my flask app.

Code: setup.py

from setuptools import find_packages, setup

setup(
    name='blog-admin-api',
    version='0.0.1',
    description="apis for blog admin",
    packages=find_packages(exclude=[]),
    install_requires=[],
    zip_safe=True,
    license='None',
    entry_points={
        'console_scripts': ['blog_admin_api=blog_admin_api.app:cli']
    }
)

Code: app.py

from flask.cli import FlaskGroup, click
from flask import Flask, Blueprint
from flask_restplus import Api, Namespace, Resource

class TestException(Exception):
    pass

def create_app():
    app = Flask(
        __name__,
        instance_relative_config=True,
        instance_path=_default_instance_path)

    @app.errorhandler(Exception)
    def handle_test_exception(error):
        return {'message': 'Error Handler!'}, 200

    api = Namespace('cat')
    @api.route('/cat')
    class Cat(Resource):
        def get(self):
            # raise TestException()
            return 'hello cat'

    cat_blueprint = Blueprint(
        'cat_blueprint', __name__, static_folder='static')
    cat_api = Api(cat_blueprint, version='0.0.1')
    cat_api.add_namespace(api, path='/c')

    app.register_blueprint(cat_blueprint)

    return app

@click.group(cls=FlaskGroup, create_app=create_app)
def cli():
    pass

Repro Steps

  1. pip install -e .
  2. Run my app by command: blog_admin_api run --debugger --reload --with-threads
  3. open Chrome
  4. go to http://localhost:5000/c/cat

image

  1. go to a 404 link: http://localhost:5000/c/cat/kkk

image

(Error handler is working now.)

  1. modify namespace code
@api.route('/cat')
class Cat(Resource):
    def get(self):
        raise TestException()
        return 'hello cat'
  1. go to http://localhost:5000/c/cat

image

(Error handler is not working.)

Environment

jschwindt commented 4 years ago

The @api.errorhandler decorator does not work in 0.13.0. If I go back to version 0.12.1 it works perfectly.

MyJoiT commented 4 years ago

The @api.errorhandler decorator does not work in 0.13.0. If I go back to version 0.12.1 it works perfectly.

@jschwindt

Namespace.errorhandler and Blueprint.errorhandler work perfectly in my app with 0.13.0 but Flask.errorhandler does not work