From 19bede4fc7f7320bb4eb16c3fe1a211b19ab4714 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Fri, 26 Jun 2020 12:27:51 +0200 Subject: Revamp error responses everywhere --- opendc/models/model.py | 7 +++---- opendc/models/simulation.py | 7 +++---- opendc/models/topology.py | 7 +++---- opendc/models/user.py | 18 +++++------------- 4 files changed, 14 insertions(+), 25 deletions(-) (limited to 'opendc/models') diff --git a/opendc/models/model.py b/opendc/models/model.py index d0bf34ee..2505ae61 100644 --- a/opendc/models/model.py +++ b/opendc/models/model.py @@ -1,4 +1,5 @@ from opendc.util.database import DB +from opendc.util.exceptions import ClientError from opendc.util.rest import Response @@ -12,11 +13,9 @@ class Model: def __init__(self, obj): self.obj = obj - def validate(self, request_google_id=None): + def check_exists(self): if self.obj is None: - return Response(404, 'Not found.') - - return None + raise ClientError(Response(404, 'Not found.')) def set_property(self, key, value): self.obj[key] = value diff --git a/opendc/models/simulation.py b/opendc/models/simulation.py index f58581cf..5cd3d49e 100644 --- a/opendc/models/simulation.py +++ b/opendc/models/simulation.py @@ -1,16 +1,15 @@ from opendc.models.model import Model from opendc.models.user import User +from opendc.util.exceptions import ClientError from opendc.util.rest import Response class Simulation(Model): collection_name = 'simulations' - def validate_user_access(self, google_id, edit_access): + def check_user_access(self, google_id, edit_access): user = User.from_google_id(google_id) authorizations = list( filter(lambda x: str(x['simulationId']) == str(self.obj['_id']), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): - return Response(403, "Forbidden from retrieving simulation.") - - return None + raise ClientError(Response(403, "Forbidden from retrieving simulation.")) diff --git a/opendc/models/topology.py b/opendc/models/topology.py index b01d5f41..6dde3e2a 100644 --- a/opendc/models/topology.py +++ b/opendc/models/topology.py @@ -1,16 +1,15 @@ from opendc.models.model import Model from opendc.models.user import User +from opendc.util.exceptions import ClientError from opendc.util.rest import Response class Topology(Model): collection_name = 'topologies' - def validate_user_access(self, google_id, edit_access): + def check_user_access(self, google_id, edit_access): user = User.from_google_id(google_id) authorizations = list( filter(lambda x: str(x['topologyId']) == str(self.obj['_id']), user.obj['authorizations'])) if len(authorizations) == 0 or (edit_access and authorizations[0]['authorizationLevel'] == 'VIEW'): - return Response(403, "Forbidden from retrieving topology.") - - return None \ No newline at end of file + raise ClientError(Response(403, "Forbidden from retrieving topology.")) diff --git a/opendc/models/user.py b/opendc/models/user.py index ea8b1f3f..cd314457 100644 --- a/opendc/models/user.py +++ b/opendc/models/user.py @@ -1,5 +1,6 @@ from opendc.models.model import Model from opendc.util.database import DB +from opendc.util.exceptions import ClientError from opendc.util.rest import Response @@ -14,21 +15,12 @@ class User(Model): def from_google_id(cls, google_id): return User(DB.fetch_one({'googleId': google_id}, User.collection_name)) - def validate(self, request_google_id=None): - super_validation = super().validate(request_google_id) - - if super_validation is not None: - return super_validation - + def check_correct_user(self, request_google_id): if request_google_id is not None and self.obj['googleId'] != request_google_id: - return Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.') + raise ClientError(Response(403, f'Forbidden from editing user with ID {self.obj["_id"]}.')) - return None - - def validate_insertion(self): + def check_already_exists(self): existing_user = DB.fetch_one({'googleId': self.obj['googleId']}, self.collection_name) if existing_user is not None: - return Response(409, f'User already exists.') - - return None + raise ClientError(Response(409, 'User already exists.')) -- cgit v1.2.3