Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
1. added HTTPException for auth functions.
Browse files Browse the repository at this point in the history
2. fixed bug in exception handling.
  • Loading branch information
mostafa committed Jan 22, 2018
1 parent f61d16f commit 4629a15
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions grest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ class Relation(NodeAndRelationHelper):
pass


class HTTPException(Exception):

"""
Base class for all http exceptions raised in auth functions
"""

def __init__(self, message, status_code):
self.message = message
self.status_code = status_code

def __str__(self):
return self.message


def authenticate(func):
@wraps(func)
def authenticate_requests(self, *args, **kwargs):
Expand All @@ -142,8 +156,10 @@ def authenticate_requests(self, *args, **kwargs):
try:
app.authentication_function(self)
return func(self, *args, **kwargs)
except Exception as e:
except HTTPException as e:
return jsonify(errors=[e.message]), e.status_code
except Exception as e:
return jsonify(errors=[e.message]), 500
else:
return func(self, *args, **kwargs)
return authenticate_requests
Expand All @@ -165,8 +181,10 @@ def authorize_requests(self, *args, **kwargs):
try:
app.authorization_function(self)
return func(self, *args, **kwargs)
except Exception as e:
except HTTPException as e:
return jsonify(errors=[e.message]), e.status_code
except Exception as e:
return jsonify(errors=[e.message]), 500
else:
return func(self, *args, **kwargs)
return authorize_requests
Expand Down

0 comments on commit 4629a15

Please sign in to comment.