Skip to content

Commit b0c0cde

Browse files
committed
Add error handler
1 parent cf6f2d7 commit b0c0cde

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

01-Login/server.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from flask import request
1010
from flask import session
1111
from flask import redirect
12+
from flask import jsonify
1213
from flask_oauthlib.client import OAuth
1314
from functools import wraps
1415
from six.moves.urllib.parse import urlencode
@@ -30,6 +31,21 @@
3031
APP = Flask(__name__, static_url_path='/public', static_folder='./public')
3132
APP.secret_key = constants.SECRET_KEY
3233
APP.debug = True
34+
35+
36+
# Format error response and append status code.
37+
class AuthError(Exception):
38+
def __init__(self, error, status_code):
39+
self.error = error
40+
self.status_code = status_code
41+
42+
43+
@APP.errorhandler(Exception)
44+
def handle_auth_error(ex):
45+
response = jsonify(ex.error)
46+
response.status_code = ex.status_code
47+
return response
48+
3349
oauth = OAuth(APP)
3450

3551

@@ -67,10 +83,7 @@ def home():
6783
def callback_handling():
6884
resp = auth0.authorized_response()
6985
if resp is None:
70-
raise Exception('Access denied: reason=%s error=%s' % (
71-
request.args['error_reason'],
72-
request.args['error_description']
73-
))
86+
raise AuthError({'code': request.args['error'], 'description': request.args['error_description']}, 401)
7487

7588
# Obtain JWT and the keys to validate the signature
7689
idToken = resp['id_token']

0 commit comments

Comments
 (0)