gpt4 book ai didi

python-3.x - 捕获 Flask API 中引发的自定义异常。引发的所有异常都以 500 错误告终

转载 作者:行者123 更新时间:2023-12-03 16:00:13 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Catch an exception and displaying a custom error page

(1 个回答)


2年前关闭。




我希望能够在我的 API 中引发验证和其他异常,并在包装​​ View 中捕获它们,该 View 将以 JSON 形式返回错误消息。

我以为我可以使用这样的东西:

异常(exception)

class APIException(Exception):

def __init__(self, message, status_code=406):
super().__init__(message)
self.status_code = status_code

捕捉异常
# Todo: find a better way of handling this. Flask should have some way of handling exceptions better than this
def catch_errors(view):
@functools.wraps(view)
def wrapped_view(**kwargs):
try:
return view(**kwargs)
except APIException as e:
# It seems to hit here
return json_response({'message': str(e)}, e.status_code)
except Exception as e:
# But bubbles up to here and returns this
return json_response({'message': str(e)}, 500)

return wrapped_view

路线
@router.route('/a-route', methods=['POST'])
@catch_errors
def get():
return json_response(ARouteAPI().post(request.get_json()))

API处理岗位
class ARouteAPI():

def post(data):
if not data.something:
raise APIException("Invalid data error")

我遇到的问题是,无论我抛出什么异常,它都会冒泡到一个完整的 Exception并点击 500所以它永远不会返回 APIException .

有谁知道为什么?如何解决?

或者有没有更好的方法来处理这个问题?

更新

仍然为此做噩梦。

处理它的更好方法是使用 @app.errorhandler装饰器(在我的例子中 @router 是我的蓝图名称,所以我使用 @router.errorhandler 代替),正如@Hyunwoo 所建议的那样。

但是,无论抛出什么异常,它最终都会遇到 500 错误,我不知道为什么。

我发现了类似的示例,其中 Debug模式导致问题重新出现,我认为这可能会导致问题,但我已将 Debug模式设置为 false
错误处理程序
router = Blueprint('router', __name__)


@router.errorhandler(APIException)
def api_exception_handler(e):
return jsonify({'message': str(e)}, e.status_code), 400

@router.errorhandler(500)
def error_handler(e):
return jsonify({'message': str(e)}), 500 # Always hits this whatever exception is raised

堆栈跟踪
[2018-10-16 23:48:39,767] ERROR in app: Exception on /drone/7 [PUT]
Traceback (most recent call last):
File "/home/sarcoma/PycharmProjects/drone_squadron/venv/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/sarcoma/PycharmProjects/drone_squadron/venv/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/sarcoma/PycharmProjects/drone_squadron/venv/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/home/sarcoma/PycharmProjects/drone_squadron/venv/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/sarcoma/PycharmProjects/drone_squadron/venv/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/sarcoma/PycharmProjects/drone_squadron/venv/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/sarcoma/PycharmProjects/drone_squadron/venv/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/sarcoma/PycharmProjects/drone_squadron/drone_squadron/router.py", line 62, in wrapped_view
return view(**kwargs)
File "/home/sarcoma/PycharmProjects/drone_squadron/drone_squadron/router.py", line 159, in drone_detail
return JsonRequestHandler.detail(DroneApi(), item_id)
File "/home/sarcoma/PycharmProjects/drone_squadron/drone_squadron/request/json_request_handler.py", line 39, in detail
return json_response(api.put(item_id, request.get_json()))
File "/home/sarcoma/PycharmProjects/drone_squadron/drone_squadron/api/drone_api.py", line 42, in put
raise APIException("Not enough scrap")
drone_squadron.exception.exceptions.APIException: Not enough scrap
127.0.0.1 - - [16/Oct/2018 23:48:39] "PUT /drone/7 HTTP/1.1" 500 -

错误处理程序规范

这是 print(app.error_handler_spec) 的输出正如@Hyunwoo 所建议的那样。
{None: {
500: {<class 'werkzeug.exceptions.InternalServerError'>: <function error_handler at 0x7fdd0cb6ad08>},
None: {
<class 'sqlalchemy.exc.IntegrityError'>: <function integrity_error_handler at 0x7fdd0cb6ab70>,
<class 'exception.exceptions.APIException'>: <function api_exception_handler at 0x7fdd0cb6abf8>,
<class 'exception.exceptions.ValidationException'>: <function validation_exception_handler at 0x7fdd0cb6ac80>
}}}

最佳答案

我用过 app.errorhandler用于处理 flask 中的错误。 (不管是自定义错误,还是标准错误)

# IntegrityError Error handler
@app.errorhandler(IntegrityError)
def exception_handler(e):
return jsonify({'message': e._message().split('"')[2].strip()}), 400


# Custom Error handler

# Duplicated column value
@app.errorhandler(APIException)
def exception_handler(e):
return jsonify({'message': e.description}), 400

并在 View 中使用相同的用法
@app.route('/')
def index():
if something_wrong():
raise APIException

class ARouteAPI():
def post(data):
if not data.something:
raise APIException("Invalid data error")

不要忘记确保您的处理程序是由 flask 应用程序添加的
>>> print(app.error_handler_spec)
{None: {None: {<class 'sqlalchemy.exc.IntegrityError'>: <function exception_handler at 0x10ae24158>,
<class 'app.error.exc.DuplicatedValueError'>: <function exception_handler at 0x10ae54268>,
<class 'app.error.exc.WrongSelectionError'>: <function exception_handler at 0x10ae542f0>},
404: {<class 'werkzeug.exceptions.NotFound'>: <function exception_handler at 0x10a53c7b8>}}}

关于python-3.x - 捕获 Flask API 中引发的自定义异常。引发的所有异常都以 500 错误告终,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52834815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com