python-restx / flask-restx

Fork of Flask-RESTPlus: Fully featured framework for fast, easy and documented API development with Flask
https://flask-restx.readthedocs.io/en/latest/
Other
2.16k stars 335 forks source link

Error handler regiter on one namespace, but actually the error handler was registered on all namespaces. #268

Open YishuiLi opened 3 years ago

YishuiLi commented 3 years ago

Ask a question Error handler regiter on one namespace, but actually the error handler was registered on all namespaces.!

Environment

Code

from flask import Blueprint
from flask_restx import Api

from app.api.useraction import user_ns
from app.api.engine import engine_ns

api_v1 = Blueprint('api', __name__, url_prefix='/')

engine_ns = Namespace('engine', description='Engine Namespace')
user_ns = Namespace('user', description='User Namespace')

api = Api(
    api_v1,
    version='1.0',
    title='test',
    description='test',
)

api.add_namespace(user_ns)
api.add_namespace(engine_ns)
from app.base_api import api
from app.api.useraction import user_ns
from app.api.engine import engine_ns

@api.errorhandler(Exception)
def en_handle_exception(error):
    error_msg = str(error)
    return {"message": error_msg}, 500

@engine_ns.errorhandler(Exception)
def engine_exception(error):
    error_msg = "engine exception"
    return {"message": error_msg}, 500

@user_ns.errorhandler(Exception)
def user_exception(error):
    error_msg = "user exception"
    return {"message": error_msg}, 500

Result When I call the api in the Engine Namespace, the engine error handler was triggered. image

But when I call the api in the User Namespace, the engine error handler was also triggered and the user error handler wasn't triggered. image

Expected result When I call the api in the Engine Namespace, the engine error handler was triggered. image

When I call the api in the User Namespace, the user error handler was triggered. image

santalvarez commented 3 years ago

This happens on version 0.4.0 too. Are there any plans to fix it? Do you guys know where the bug is? Maybe I can fix it

luketflp commented 1 month ago

Any news about this issue ?