[英]Error while Handling Custom Exceptions in Flask (Python)
在我的路线Flask
文件中,我import
exception.py
@app.errorhandler(exception.BadRequest)
def BadRequest(e):
abort(400)
@app.errorhandler(exception.Unauthorized)
def Unauthorized(e):
abort(401)
@app.errorhandler(exception.Forbidden)
def Forbidden(e):
abort(404)
@app.route("/", methods=['GET'])
def index():
if(request.args.get('dev') == 'windyEw336'):
return 'ok', 200
else:
raise exception.Unauthorized('Key is not valid!')
在exception.py
我有:
class Error(Exception):
def __init__(self, *args, **kwargs):
pass
class BadRequest(Error):
pass
class Unauthorized(Error):
pass
class Forbidden(Error):
pass
并收到以下错误。
Mar 26 11:04:34 gunicorn[20507]: [2020-03-26 11:04:34 +0000] [20532] [ERROR] Error handling request /?dev=windyEw336.
Mar 26 11:04:34 gunicorn[20507]: Traceback (most recent call last):
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
Mar 26 11:04:34 gunicorn[20507]: rv = self.dispatch_request()
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
Mar 26 11:04:34 gunicorn[20507]: return self.view_functions[rule.endpoint](**req.view_args)
Mar 26 11:04:34 gunicorn[20507]: File "/uni/erp/erp.py", line 34, in index
Mar 26 11:04:34 gunicorn[20507]: raise exception.Unauthorized('Key is not valid!')
Mar 26 11:04:34 gunicorn[20507]: exception.Unauthorized: Key is not valid!
Mar 26 11:04:34 gunicorn[20507]: During handling of the above exception, another exception occurred:
Mar 26 11:04:34 gunicorn[20507]: Traceback (most recent call last):
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in handle
Mar 26 11:04:34 gunicorn[20507]: self.handle_request(listener, req, client, addr)
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
Mar 26 11:04:34 gunicorn[20507]: respiter = self.wsgi(environ, resp.start_response)
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/app.py", line 2463, in __call__
Mar 26 11:04:34 gunicorn[20507]: return self.wsgi_app(environ, start_response)
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/app.py", line 2449, in wsgi_app
Mar 26 11:04:34 gunicorn[20507]: response = self.handle_exception(e)
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/app.py", line 1866, in handle_exception
Mar 26 11:04:34 gunicorn[20507]: reraise(exc_type, exc_value, tb)
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
Mar 26 11:04:34 gunicorn[20507]: raise value
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
Mar 26 11:04:34 gunicorn[20507]: response = self.full_dispatch_request()
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
Mar 26 11:04:34 gunicorn[20507]: rv = self.handle_user_exception(e)
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
Mar 26 11:04:34 gunicorn[20507]: return handler(e)
Mar 26 11:04:34 gunicorn[20507]: File "/uni/erp/erp.py", line 22, in Unauthorized
Mar 26 11:04:34 gunicorn[20507]: abort(401)
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/werkzeug/exceptions.py", line 822, in abort
Mar 26 11:04:34 gunicorn[20507]: return _aborter(status, *args, **kwargs)
Mar 26 11:04:34 gunicorn[20507]: File "/pyenv/lib/python3.8/site-packages/werkzeug/exceptions.py", line 807, in __call__
Mar 26 11:04:34 gunicorn[20507]: raise self.mapping[code](*args, **kwargs)
Mar 26 11:04:34 gunicorn[20507]: werkzeug.exceptions.Unauthorized: 401 Unauthorized: The server could not verify that you are authorized to access the URL requested. You either supplied the wrong credentials (e.g. a bad password), or your browser doesn't understand how to supply the credentials required.
我究竟做错了什么?
您正在处理abort(401)
的错误处理程序中调用abort(401)
@app.errorhandler(exception.Unauthorized)
应该处理未经授权的异常,但是你通过调用abort(401)
来抛出一个新的未经授权的异常
我建议在此页面上阅读有关 Flask 中错误处理程序的信息:https ://flask.palletsprojects.com/en/1.1.x/patterns/errorpages/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.