Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
MyJoiT opened this issue Nov 24, 2019 · 2 comments
Labels

Comments

@MyJoiT
Copy link

MyJoiT commented Nov 24, 2019

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

  • OS: Windows 10
  • Python version: 3.7.5
  • Flask version: 1.1.1
  • Flask-RESTPlus 0.13.0
@MyJoiT MyJoiT added the bug label Nov 24, 2019
@jschwindt
Copy link

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

@MyJoiT
Copy link
Author

MyJoiT commented Nov 26, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants