在Flask(Python)中处理自定义异常时出错

2024-04-19 03:40:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的路由Flask文件中import{}

@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!')

在{}我有:

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.

我做错了什么


Tags: inpyselfapppyenvrequestlibpackages