捕获Flask API中引发的自定义异常。引发的所有异常都将导致500错误

时间:2018-10-16 11:49:14

标签: python-3.x exception flask

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

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

异常

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错误,我也不知道为什么。

我发现了类似的示例,其中调试模式导致问题重发,我以为可能会引起问题,但是我将调试模式设置为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

StackTrace

[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 -

错误处理程序规范

这是@Hyunwoo建议的print(app.error_handler_spec)的输出。

{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>
}}}

1 个答案:

答案 0 :(得分:4)

我用app.errorhandler处理了烧瓶中的错误。 (无论是自定义错误还是标准错误)

# 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

与视图中的用法相同

@app.route('/')
def index():
    if something_wrong():
        raise APIException

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

别忘了确保烧瓶应用添加了处理程序

>>> 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>}}}