Skip to content

Commit

Permalink
Merge pull request #8768 from OpenMined/error_handling_api_endpoints
Browse files Browse the repository at this point in the history
Added API Endpoint error handling
  • Loading branch information
teo-milea committed Apr 26, 2024
2 parents 948fa1a + b2cc6fb commit 3e95f47
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions packages/syft/src/syft/service/api/api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# third party
from pydantic import ValidationError
from result import Err
from result import Ok

# relative
Expand Down Expand Up @@ -418,15 +419,23 @@ def call(

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()}")
try:
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(
Expand All @@ -450,15 +459,23 @@ def call_public(

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()}")
try:
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
Expand Down Expand Up @@ -486,13 +503,22 @@ def call_private(
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()}")
try:
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",
Expand Down

0 comments on commit 3e95f47

Please sign in to comment.