1

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?

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.