Skip to content

Commit

Permalink
defined ClientAppException
Browse files Browse the repository at this point in the history
  • Loading branch information
jafermarq committed Apr 2, 2024
1 parent 219e610 commit 9fc02fb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
9 changes: 7 additions & 2 deletions src/py/flwr/client/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from grpc import RpcError

from flwr.client.client import Client
from flwr.client.client_app import ClientApp, LoadClientAppError
from flwr.client.client_app import ClientApp, ClientAppException, LoadClientAppError
from flwr.client.typing import ClientFn
from flwr.common import GRPC_MAX_MESSAGE_LENGTH, EventType, Message, event
from flwr.common.address import parse_address
Expand All @@ -34,6 +34,7 @@
TRANSPORT_TYPE_GRPC_RERE,
TRANSPORT_TYPE_REST,
TRANSPORT_TYPES,
ErrorCode,
)
from flwr.common.exit_handlers import register_exit_handlers
from flwr.common.logger import log, warn_deprecated_feature
Expand Down Expand Up @@ -507,11 +508,15 @@ def _load_client_app() -> ClientApp:

# Don't update/change NodeState

e_code = ErrorCode.UNKNOWN
if isinstance(ex, ClientAppException):
e_code = ErrorCode.CLIENT_APP_RAISED_EXCEPTION

# Create error message
# Reason example: "<class 'ZeroDivisionError'>:<'division by zero'>"
reason = str(type(ex)) + ":<'" + str(ex) + "'>"
reply_message = message.create_error_reply(
error=Error(code=0, reason=reason)
error=Error(code=e_code, reason=reason)
)

# Send
Expand Down
52 changes: 32 additions & 20 deletions src/py/flwr/client/client_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@
from .typing import ClientAppCallable


class ClientAppException(Exception):
"""Exepction raised when an exception is raised while executing a ClientApp."""

def __init__(self, message: str):
ex_name = self.__class__.__name__
self.message = f"\nA {ex_name} occurred." + message
super().__init__(self.message)


class ClientApp:
"""Flower ClientApp.
Expand Down Expand Up @@ -84,26 +93,29 @@ def ffn(

def __call__(self, message: Message, context: Context) -> Message:
"""Execute `ClientApp`."""
# Execute message using `client_fn`
if self._call:
return self._call(message, context)

# Execute message using a new
if message.metadata.message_type == MessageType.TRAIN:
if self._train:
return self._train(message, context)
raise ValueError("No `train` function registered")
if message.metadata.message_type == MessageType.EVALUATE:
if self._evaluate:
return self._evaluate(message, context)
raise ValueError("No `evaluate` function registered")
if message.metadata.message_type == MessageType.QUERY:
if self._query:
return self._query(message, context)
raise ValueError("No `query` function registered")

# Message type did not match one of the known message types abvoe
raise ValueError(f"Unknown message_type: {message.metadata.message_type}")
try:
# Execute message using `client_fn`
if self._call:
return self._call(message, context)

# Execute message using a new
if message.metadata.message_type == MessageType.TRAIN:
if self._train:
return self._train(message, context)
raise ValueError("No `train` function registered")
if message.metadata.message_type == MessageType.EVALUATE:
if self._evaluate:
return self._evaluate(message, context)
raise ValueError("No `evaluate` function registered")
if message.metadata.message_type == MessageType.QUERY:
if self._query:
return self._query(message, context)
raise ValueError("No `query` function registered")

# Message type did not match one of the known message types abvoe
raise ValueError(f"Unknown message_type: {message.metadata.message_type}")
except Exception as ex:
raise ClientAppException(str(ex)) from ex

def train(self) -> Callable[[ClientAppCallable], ClientAppCallable]:
"""Return a decorator that registers the train fn with the client app.
Expand Down

0 comments on commit 9fc02fb

Please sign in to comment.