Skip to content

Commit

Permalink
Merge pull request #8078 from OpenMined/init_exception_handling
Browse files Browse the repository at this point in the history
Init exception handling
  • Loading branch information
tcp authored Oct 4, 2023
2 parents f359273 + 3ea2d62 commit feb4961
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
Empty file.
27 changes: 27 additions & 0 deletions packages/syft/src/syft/exceptions/exception.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# stdlib
from typing import List
from typing import Optional

# relative
from ..service.context import NodeServiceContext
from ..service.response import SyftError
from ..service.user.user_roles import ServiceRole


class PySyftException(Exception):
"""Base class for all PySyft exceptions."""

def __init__(self, message: str, roles: Optional[List[ServiceRole]] = None):
super().__init__(message)
self.message = message
self.roles = roles if roles else [ServiceRole.ADMIN]

def raise_with_context(self, context: NodeServiceContext):
self.context = context
return self

def handle(self) -> SyftError:
# if self.context and self.context.role in self.roles:
return SyftError(message=self.message)
# else:
# return SyftError(message="Access denied to exception message.")
9 changes: 9 additions & 0 deletions packages/syft/src/syft/exceptions/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# stdlib

# relative
from ..service.user.user_roles import ServiceRole
from .exception import PySyftException

UserAlreadyExistsException = PySyftException(
message="User already exists", roles=[ServiceRole.ADMIN]
)
3 changes: 3 additions & 0 deletions packages/syft/src/syft/node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from ..client.api import SyftAPICall
from ..client.api import SyftAPIData
from ..client.api import debox_signed_syftapicall_response
from ..exceptions.exception import PySyftException
from ..external import OBLV
from ..serde.deserialize import _deserialize
from ..serde.serialize import _serialize
Expand Down Expand Up @@ -806,6 +807,8 @@ def handle_api_call_with_unsigned_result(
method = self.get_service_method(_private_api_path)
try:
result = method(context, *api_call.args, **api_call.kwargs)
except PySyftException as e:
return e.handle()
except Exception:
result = SyftError(
message=f"Exception calling {api_call.path}. {traceback.format_exc()}"
Expand Down
5 changes: 2 additions & 3 deletions packages/syft/src/syft/service/user/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

# relative
from ...abstract_node import NodeType
from ...exceptions.user import UserAlreadyExistsException
from ...node.credentials import SyftSigningKey
from ...node.credentials import SyftVerifyKey
from ...node.credentials import UserLoginCredentials
Expand Down Expand Up @@ -235,9 +236,7 @@ def update(
email=user_update.email
)
if user_with_email_exists:
return SyftError(
message=f"A user with the email {user_update.email} already exists."
)
raise UserAlreadyExistsException.raise_with_context(context=context)

if result.is_err():
error_msg = (
Expand Down
29 changes: 9 additions & 20 deletions packages/syft/tests/syft/users/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@ def test_guest_user_update_to_root_email_failed(
uid=client.me.id, user_update=user_update_to_root_email
)
assert isinstance(res, SyftError)
assert (
res.message == f"A user with the email {default_root_email} already exists."
)
assert res.message == "User already exists"


def test_user_view_set_password(worker: Worker, root_client: DomainClient) -> None:
Expand Down Expand Up @@ -299,36 +297,27 @@ def test_user_view_set_default_admin_email_failed(
default_root_email = get_default_root_email()
result = ds_client.me.set_email(default_root_email)
assert isinstance(result, SyftError)
assert (
result.message == f"A user with the email {default_root_email} already exists."
)
assert result.message == "User already exists"

result_2 = guest_client.me.set_email(default_root_email)
assert isinstance(result_2, SyftError)
assert (
result.message == f"A user with the email {default_root_email} already exists."
)
assert result_2.message == "User already exists"


def test_user_view_set_duplicated_email(
root_client: DomainClient, ds_client: DomainClient, guest_client: DomainClient
) -> None:
result = ds_client.me.set_email(root_client.me.email)
result2 = guest_client.me.set_email(root_client.me.email)

assert isinstance(result, SyftError)
assert (
result.message
== f"A user with the email {root_client.me.email} already exists."
)
assert result.message == "User already exists"
assert isinstance(result2, SyftError)
assert (
result2.message
== f"A user with the email {root_client.me.email} already exists."
)
assert result2.message == "User already exists"

result3 = guest_client.me.set_email(ds_client.me.email)
assert isinstance(result3, SyftError)
assert (
result3.message == f"A user with the email {ds_client.me.email} already exists."
)
assert result3.message == "User already exists"


def test_user_view_update_name_institution_website(
Expand Down

0 comments on commit feb4961

Please sign in to comment.