From 0eb8dbc3c78fa5af814d42d9a43cc058ab641fe8 Mon Sep 17 00:00:00 2001 From: "panxuchen.pxc" Date: Tue, 14 May 2024 19:56:48 +0800 Subject: [PATCH] make error message more specific --- src/agentscope/agents/rpc_agent.py | 10 +++---- src/agentscope/rpc/__init__.py | 10 +++---- src/agentscope/rpc/rpc_agent_client.py | 10 +++---- src/agentscope/rpc/rpc_agent_pb2_grpc.py | 4 +-- src/agentscope/utils/tools.py | 33 +++++++++++++----------- 5 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/agentscope/agents/rpc_agent.py b/src/agentscope/agents/rpc_agent.py index da706214c..47d32ce3a 100644 --- a/src/agentscope/agents/rpc_agent.py +++ b/src/agentscope/agents/rpc_agent.py @@ -18,13 +18,13 @@ import grpc from grpc import ServicerContext from expiringdict import ExpiringDict -except ImportError: +except ImportError as import_error: from agentscope.utils.tools import ImportErrorReporter - dill = ImportErrorReporter("dill", "distribute") - grpc = ImportErrorReporter("grpcio", "distribute") - ServicerContext = ImportErrorReporter("grpcio", "distribute") - ExpiringDict = ImportErrorReporter("expiringdict", "distribute") + dill = ImportErrorReporter(import_error, "distribute") + grpc = ImportErrorReporter(import_error, "distribute") + ServicerContext = ImportErrorReporter(import_error, "distribute") + ExpiringDict = ImportErrorReporter(import_error, "distribute") from agentscope._init import init_process, _INIT_SETTINGS from agentscope.agents.agent import AgentBase diff --git a/src/agentscope/rpc/__init__.py b/src/agentscope/rpc/__init__.py index be90213c6..2c7703a90 100644 --- a/src/agentscope/rpc/__init__.py +++ b/src/agentscope/rpc/__init__.py @@ -8,14 +8,14 @@ from .rpc_agent_pb2_grpc import RpcAgentServicer from .rpc_agent_pb2_grpc import RpcAgentStub from .rpc_agent_pb2_grpc import add_RpcAgentServicer_to_server -except ImportError: +except ImportError as import_error: from agentscope.utils.tools import ImportErrorReporter - RpcMsg = ImportErrorReporter("protobuf", "distribute") # type: ignore[misc] - RpcAgentServicer = ImportErrorReporter("grpcio", "distribute") - RpcAgentStub = ImportErrorReporter("grpcio", "distribute") + RpcMsg = ImportErrorReporter(import_error, "distribute") # type: ignore[misc] + RpcAgentServicer = ImportErrorReporter(import_error, "distribute") + RpcAgentStub = ImportErrorReporter(import_error, "distribute") add_RpcAgentServicer_to_server = ImportErrorReporter( - "grpcio", + import_error, "distribute", ) diff --git a/src/agentscope/rpc/rpc_agent_client.py b/src/agentscope/rpc/rpc_agent_client.py index bfce8993b..189e0895f 100644 --- a/src/agentscope/rpc/rpc_agent_client.py +++ b/src/agentscope/rpc/rpc_agent_client.py @@ -12,13 +12,13 @@ from grpc import RpcError from agentscope.rpc.rpc_agent_pb2 import RpcMsg # pylint: disable=E0611 from agentscope.rpc.rpc_agent_pb2_grpc import RpcAgentStub -except ImportError: +except ImportError as import_error: from agentscope.utils.tools import ImportErrorReporter - dill = ImportErrorReporter("dill", "distribute") - grpc = ImportErrorReporter("grpcio", "distribute") - RpcMsg = ImportErrorReporter("protobuf", "distribute") - RpcAgentStub = ImportErrorReporter("grpcio", "distribute") + dill = ImportErrorReporter(import_error, "distribute") + grpc = ImportErrorReporter(import_error, "distribute") + RpcMsg = ImportErrorReporter(import_error, "distribute") + RpcAgentStub = ImportErrorReporter(import_error, "distribute") RpcError = ImportError diff --git a/src/agentscope/rpc/rpc_agent_pb2_grpc.py b/src/agentscope/rpc/rpc_agent_pb2_grpc.py index 1fdaf48e0..4099c7027 100644 --- a/src/agentscope/rpc/rpc_agent_pb2_grpc.py +++ b/src/agentscope/rpc/rpc_agent_pb2_grpc.py @@ -3,10 +3,10 @@ """Client and server classes corresponding to protobuf-defined services.""" try: import grpc -except ImportError: +except ImportError as import_error: from agentscope.utils.tools import ImportErrorReporter - grpc = ImportErrorReporter("grpcio", "distribute") + grpc = ImportErrorReporter(import_error, "distribute") import agentscope.rpc.rpc_agent_pb2 as rpc__agent__pb2 diff --git a/src/agentscope/utils/tools.py b/src/agentscope/utils/tools.py index ca1df7d45..8ebd23777 100644 --- a/src/agentscope/utils/tools.py +++ b/src/agentscope/utils/tools.py @@ -304,28 +304,31 @@ class ImportErrorReporter: the specified extras requirement. """ - def __init__(self, package_name: str, extras_require: str = None) -> None: + def __init__(self, error: ImportError, extras_require: str = None) -> None: """Init the ImportErrorReporter. Args: - package_name (`str`): the name of the package to be imported. + error (`ImportError`): the original ImportError. extras_require (`str`): the extras requirement. """ - self.package_name = package_name + self.error = error self.extras_require = extras_require - def raise_error(self) -> Any: - """Raise an ImportError.""" - msg = f"Failed to import {self.package_name}." - if self.extras_require is not None: - msg += ( - f" Please install [{self.extras_require}] version of" - " agentscope." - ) - raise ImportError(msg) - def __call__(self, *args: Any, **kwds: Any) -> Any: - return self.raise_error() + return self._raise_import_error() def __getattr__(self, name: str) -> Any: - return self.raise_error() + return self._raise_import_error() + + def __getitem__(self, __key: Any) -> Any: + return self._raise_import_error() + + def _raise_import_error(self) -> Any: + """Raise the ImportError""" + err_msg = f"ImportError occorred: [{self.error.msg}]." + if self.extras_require is not None: + err_msg += ( + f" Please install [{self.extras_require}] version" + " of agentscope." + ) + raise ImportError(err_msg)