简体   繁体   中英

Error while Handling Custom Exceptions in Flask (Python)

In my route Flask file I 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!')

In exception.py I have:

class Error(Exception):
    def __init__(self, *args, **kwargs):
        pass

class BadRequest(Error):
    pass

class Unauthorized(Error):
    pass

class Forbidden(Error):
    pass

And getting the following error.

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.

What am I doing wrong?

You are calling abort(401) in your error handler that would handle an abort(401)

@app.errorhandler(exception.Unauthorized) should handle the unauthorized exception but instead you are throwing a new unauthorized exception by calling abort(401)

I suggest reading up on this page about error handlers in flask:https://flask.palletsprojects.com/en/1.1.x/patterns/errorpages/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM