From 92b94b59ad80329a2c99471edbf5bbdc9af1e525 Mon Sep 17 00:00:00 2001 From: Georgios Andreadis Date: Fri, 26 Jun 2020 12:17:26 +0200 Subject: Revamp error responses --- opendc/api/v2/simulations/simulationId/endpoint.py | 40 +++++++--------------- .../simulationId/topologies/endpoint.py | 5 +-- 2 files changed, 13 insertions(+), 32 deletions(-) (limited to 'opendc/api/v2/simulations/simulationId') diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py index b08cf8be..b8ae9a38 100644 --- a/opendc/api/v2/simulations/simulationId/endpoint.py +++ b/opendc/api/v2/simulations/simulationId/endpoint.py @@ -10,10 +10,7 @@ from opendc.util.rest import Response def GET(request): """Get this Simulation.""" - try: - request.check_required_parameters(path={'simulationId': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(path={'simulationId': 'string'}) simulation = Simulation.from_id(request.params_path['simulationId']) validation_error = simulation.validate() @@ -30,10 +27,7 @@ def GET(request): def PUT(request): """Update a simulation's name.""" - try: - request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(body={'simulation': {'name': 'name'}}, path={'simulationId': 'string'}) simulation = Simulation.from_id(request.params_path['simulationId']) @@ -55,30 +49,20 @@ def PUT(request): def DELETE(request): """Delete this Simulation.""" - # Make sure required parameters are there + request.check_required_parameters(path={'simulationId': 'string'}) - try: - request.check_required_parameters(path={'simulationId': 'string'}) - - except exceptions.ParameterError as e: - return Response(400, str(e)) - - # Instantiate a Simulation and make sure it exists - - simulation = Simulation.from_primary_key((request.params_path['simulationId'], )) - - if not simulation.exists(): - return Response(404, '{} not found.'.format(simulation)) + simulation = Simulation.from_id(request.params_path['simulationId']) - # Make sure this User is allowed to delete this Simulation + validation_error = simulation.validate() + if validation_error is not None: + return validation_error - if not simulation.google_id_has_at_least(request.google_id, 'OWN'): - return Response(403, 'Forbidden from deleting {}.'.format(simulation)) + access_error = simulation.validate_user_access(request.google_id, True) + if access_error is not None: + return access_error - # Delete this Simulation from the database + # FIXME cascading simulation.delete() - # Return this Simulation - - return Response(200, 'Successfully deleted {}.'.format(simulation), simulation.to_JSON()) + return Response(200, f'Successfully deleted simulation.', simulation.obj) diff --git a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py index 4104c4dd..d8467b40 100644 --- a/opendc/api/v2/simulations/simulationId/topologies/endpoint.py +++ b/opendc/api/v2/simulations/simulationId/topologies/endpoint.py @@ -12,10 +12,7 @@ def POST(request): # Make sure required parameters are there - try: - request.check_required_parameters(body={'topology': {'name': 'string'}}) - except exceptions.ParameterError as e: - return Response(400, str(e)) + request.check_required_parameters(body={'topology': {'name': 'string'}}) # Instantiate a Topology object from our request, and add some metadata -- cgit v1.2.3 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/api/v2/simulations/simulationId/endpoint.py | 26 +++++----------------- 1 file changed, 6 insertions(+), 20 deletions(-) (limited to 'opendc/api/v2/simulations/simulationId') diff --git a/opendc/api/v2/simulations/simulationId/endpoint.py b/opendc/api/v2/simulations/simulationId/endpoint.py index b8ae9a38..8d29202d 100644 --- a/opendc/api/v2/simulations/simulationId/endpoint.py +++ b/opendc/api/v2/simulations/simulationId/endpoint.py @@ -13,13 +13,9 @@ def GET(request): request.check_required_parameters(path={'simulationId': 'string'}) simulation = Simulation.from_id(request.params_path['simulationId']) - validation_error = simulation.validate() - if validation_error is not None: - return validation_error - access_error = simulation.validate_user_access(request.google_id, False) - if access_error is not None: - return access_error + simulation.check_exists() + simulation.check_user_access(request.google_id, False) return Response(200, 'Successfully retrieved simulation', simulation.obj) @@ -31,13 +27,8 @@ def PUT(request): simulation = Simulation.from_id(request.params_path['simulationId']) - validation_error = simulation.validate() - if validation_error is not None: - return validation_error - - access_error = simulation.validate_user_access(request.google_id, True) - if access_error is not None: - return access_error + simulation.check_exists() + simulation.check_user_access(request.google_id, True) simulation.set_property('name', request.params_body['simulation']['name']) simulation.set_property('datetime_last_edited', Database.datetime_to_string(datetime.now())) @@ -53,13 +44,8 @@ def DELETE(request): simulation = Simulation.from_id(request.params_path['simulationId']) - validation_error = simulation.validate() - if validation_error is not None: - return validation_error - - access_error = simulation.validate_user_access(request.google_id, True) - if access_error is not None: - return access_error + simulation.check_exists() + simulation.check_user_access(request.google_id, True) # FIXME cascading -- cgit v1.2.3