From 097e58f0a5dc56a85bd5e7cdbd3f84e1234e5e47 Mon Sep 17 00:00:00 2001 From: teo Date: Fri, 26 Apr 2024 15:56:05 +0300 Subject: [PATCH 1/2] added api call in try/catch --- .../syft/src/syft/service/api/api_service.py | 146 +++++++++++------- 1 file changed, 90 insertions(+), 56 deletions(-) diff --git a/packages/syft/src/syft/service/api/api_service.py b/packages/syft/src/syft/service/api/api_service.py index f23b6601905..feb066cc614 100644 --- a/packages/syft/src/syft/service/api/api_service.py +++ b/packages/syft/src/syft/service/api/api_service.py @@ -5,6 +5,7 @@ # third party from pydantic import ValidationError +from result import Err from result import Ok # relative @@ -404,29 +405,39 @@ def call( **kwargs: Any, ) -> SyftSuccess | SyftError: """Call a Custom API Method""" - custom_endpoint = self.get_code( - context=context, - endpoint_path=path, - ) - if isinstance(custom_endpoint, SyftError): - return custom_endpoint + try: + custom_endpoint = self.get_code( + context=context, + endpoint_path=path, + ) + if isinstance(custom_endpoint, SyftError): + return custom_endpoint - exec_result = custom_endpoint.exec(context, *args, **kwargs) + exec_result = custom_endpoint.exec(context, *args, **kwargs) - if isinstance(exec_result, SyftError): - return Ok(exec_result) + if isinstance(exec_result, SyftError): + return Ok(exec_result) - action_obj = ActionObject.from_obj(exec_result) - action_service = cast(ActionService, context.node.get_service(ActionService)) - result = action_service.set_result_to_store( - context=context, - result_action_object=action_obj, - has_result_read_permission=True, - ) - if result.is_err(): - return SyftError(message=f"Failed to set result to store: {result.err()}") + action_obj = ActionObject.from_obj(exec_result) + action_service = cast( + ActionService, context.node.get_service(ActionService) + ) + result = action_service.set_result_to_store( + context=context, + result_action_object=action_obj, + has_result_read_permission=True, + ) + if result.is_err(): + return SyftError( + message=f"Failed to set result to store: {result.err()}" + ) + + return Ok(result.ok()) + except Exception as e: + # stdlib + import traceback - return Ok(result.ok()) + return Err(value=f"Failed to run. {e}, {traceback.format_exc()}") @service_method(path="api.call_public", name="call_public", roles=GUEST_ROLE_LEVEL) def call_public( @@ -437,28 +448,38 @@ def call_public( **kwargs: Any, ) -> ActionObject | SyftError: """Call a Custom API Method in public mode""" - custom_endpoint = self.get_code( - context=context, - endpoint_path=path, - ) - if isinstance(custom_endpoint, SyftError): - return custom_endpoint - exec_result = custom_endpoint.exec_mock_function(context, *args, **kwargs) + try: + custom_endpoint = self.get_code( + context=context, + endpoint_path=path, + ) + if isinstance(custom_endpoint, SyftError): + return custom_endpoint + exec_result = custom_endpoint.exec_mock_function(context, *args, **kwargs) - if isinstance(exec_result, SyftError): - return Ok(exec_result) + if isinstance(exec_result, SyftError): + return Ok(exec_result) - action_obj = ActionObject.from_obj(exec_result) - action_service = cast(ActionService, context.node.get_service(ActionService)) - result = action_service.set_result_to_store( - context=context, - result_action_object=action_obj, - has_result_read_permission=True, - ) - if result.is_err(): - return SyftError(message=f"Failed to set result to store: {result.err()}") + action_obj = ActionObject.from_obj(exec_result) + action_service = cast( + ActionService, context.node.get_service(ActionService) + ) + result = action_service.set_result_to_store( + context=context, + result_action_object=action_obj, + has_result_read_permission=True, + ) + if result.is_err(): + return SyftError( + message=f"Failed to set result to store: {result.err()}" + ) - return Ok(result.ok()) + return Ok(result.ok()) + except Exception as e: + # stdlib + import traceback + + return Err(value=f"Failed to run. {e}, {traceback.format_exc()}") @service_method( path="api.call_private", name="call_private", roles=GUEST_ROLE_LEVEL @@ -470,29 +491,42 @@ def call_private( *args: Any, **kwargs: Any, ) -> ActionObject | SyftError: - """Call a Custom API Method in private mode""" - custom_endpoint = self.get_code( - context=context, - endpoint_path=path, - ) - if isinstance(custom_endpoint, SyftError): - return custom_endpoint + try: + """Call a Custom API Method in private mode""" + custom_endpoint = self.get_code( + context=context, + endpoint_path=path, + ) + if isinstance(custom_endpoint, SyftError): + return custom_endpoint - exec_result = custom_endpoint.exec_private_function(context, *args, **kwargs) + exec_result = custom_endpoint.exec_private_function( + context, *args, **kwargs + ) - if isinstance(exec_result, SyftError): - return Ok(exec_result) + if isinstance(exec_result, SyftError): + return Ok(exec_result) - action_obj = ActionObject.from_obj(exec_result) + action_obj = ActionObject.from_obj(exec_result) - action_service = cast(ActionService, context.node.get_service(ActionService)) - result = action_service.set_result_to_store( - context=context, result_action_object=action_obj - ) - if result.is_err(): - return SyftError(message=f"Failed to set result to store: {result.err()}") + action_service = cast( + ActionService, context.node.get_service(ActionService) + ) + result = action_service.set_result_to_store( + context=context, result_action_object=action_obj + ) + if result.is_err(): + return SyftError( + message=f"Failed to set result to store: {result.err()}" + ) + + return Ok(result.ok()) + + except Exception as e: + # stdlib + import traceback - return Ok(result.ok()) + return Err(value=f"Failed to run. {e}, {traceback.format_exc()}") @service_method( path="api.exists", From 0180d43cfc491ba46d23f8c324bdc036f8a113a3 Mon Sep 17 00:00:00 2001 From: teo Date: Fri, 26 Apr 2024 16:16:05 +0300 Subject: [PATCH 2/2] restrict try/catch scope --- .../syft/src/syft/service/api/api_service.py | 82 +++++++++---------- 1 file changed, 37 insertions(+), 45 deletions(-) diff --git a/packages/syft/src/syft/service/api/api_service.py b/packages/syft/src/syft/service/api/api_service.py index feb066cc614..bb5a8cedad7 100644 --- a/packages/syft/src/syft/service/api/api_service.py +++ b/packages/syft/src/syft/service/api/api_service.py @@ -405,23 +405,21 @@ def call( **kwargs: Any, ) -> SyftSuccess | SyftError: """Call a Custom API Method""" - try: - custom_endpoint = self.get_code( - context=context, - endpoint_path=path, - ) - if isinstance(custom_endpoint, SyftError): - return custom_endpoint + custom_endpoint = self.get_code( + context=context, + endpoint_path=path, + ) + if isinstance(custom_endpoint, SyftError): + return custom_endpoint - exec_result = custom_endpoint.exec(context, *args, **kwargs) + exec_result = custom_endpoint.exec(context, *args, **kwargs) - if isinstance(exec_result, SyftError): - return Ok(exec_result) + if isinstance(exec_result, SyftError): + return Ok(exec_result) - action_obj = ActionObject.from_obj(exec_result) - action_service = cast( - ActionService, context.node.get_service(ActionService) - ) + action_obj = ActionObject.from_obj(exec_result) + action_service = cast(ActionService, context.node.get_service(ActionService)) + try: result = action_service.set_result_to_store( context=context, result_action_object=action_obj, @@ -448,22 +446,20 @@ def call_public( **kwargs: Any, ) -> ActionObject | SyftError: """Call a Custom API Method in public mode""" - try: - custom_endpoint = self.get_code( - context=context, - endpoint_path=path, - ) - if isinstance(custom_endpoint, SyftError): - return custom_endpoint - exec_result = custom_endpoint.exec_mock_function(context, *args, **kwargs) + custom_endpoint = self.get_code( + context=context, + endpoint_path=path, + ) + if isinstance(custom_endpoint, SyftError): + return custom_endpoint + exec_result = custom_endpoint.exec_mock_function(context, *args, **kwargs) - if isinstance(exec_result, SyftError): - return Ok(exec_result) + if isinstance(exec_result, SyftError): + return Ok(exec_result) - action_obj = ActionObject.from_obj(exec_result) - action_service = cast( - ActionService, context.node.get_service(ActionService) - ) + action_obj = ActionObject.from_obj(exec_result) + action_service = cast(ActionService, context.node.get_service(ActionService)) + try: result = action_service.set_result_to_store( context=context, result_action_object=action_obj, @@ -491,27 +487,23 @@ def call_private( *args: Any, **kwargs: Any, ) -> ActionObject | SyftError: - try: - """Call a Custom API Method in private mode""" - custom_endpoint = self.get_code( - context=context, - endpoint_path=path, - ) - if isinstance(custom_endpoint, SyftError): - return custom_endpoint + """Call a Custom API Method in private mode""" + custom_endpoint = self.get_code( + context=context, + endpoint_path=path, + ) + if isinstance(custom_endpoint, SyftError): + return custom_endpoint - exec_result = custom_endpoint.exec_private_function( - context, *args, **kwargs - ) + exec_result = custom_endpoint.exec_private_function(context, *args, **kwargs) - if isinstance(exec_result, SyftError): - return Ok(exec_result) + if isinstance(exec_result, SyftError): + return Ok(exec_result) - action_obj = ActionObject.from_obj(exec_result) + action_obj = ActionObject.from_obj(exec_result) - action_service = cast( - ActionService, context.node.get_service(ActionService) - ) + action_service = cast(ActionService, context.node.get_service(ActionService)) + try: result = action_service.set_result_to_store( context=context, result_action_object=action_obj )