我是靠谱客的博主 可靠乌龟,这篇文章主要介绍AssertionError: View function mapping is overwriting an existing endpoint function,现在分享给大家,希望可以做个参考。
问题:给两个函数添加两个装饰器
复制代码
1
2
3
4
5
6
7
8
9@app.route("/path1") @exception_handler def func1(): pass @app.route("/path2") @exception_handler def func2(): pass
这样就会造成函数名的重定义。
解决:改一下包装的函数名即可:wrapper.__name__ = func.__name__
复制代码
1
2
3
4
5
6
7
8
9
10
11
12def exception_handler(func): def wrapper(*args, **kwargs): try: return func(*args, **kwargs) except Exception as e: error_code = getattr(e, "code", 500) logger.exception("Service exception: %s", e) r = dict_to_json({"message": e.message, "matches": e.message, "error_code": error_code}) return Response(r, status=error_code, mimetype='application/json') # Renaming the function name: wrapper.__name__ = func.__name__ return wrapper
最后
以上就是可靠乌龟最近收集整理的关于AssertionError: View function mapping is overwriting an existing endpoint function的全部内容,更多相关AssertionError:内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复