From 598d208e542d743f55346da3aea66157f8a0884b Mon Sep 17 00:00:00 2001 From: Yeuoly Date: Fri, 27 Dec 2024 12:09:39 +0800 Subject: [PATCH] fix: agent error handling --- api/core/plugin/manager/base.py | 9 ++++++--- api/services/agent_service.py | 6 +++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/api/core/plugin/manager/base.py b/api/core/plugin/manager/base.py index 8d881120b1..60a54b34cf 100644 --- a/api/core/plugin/manager/base.py +++ b/api/core/plugin/manager/base.py @@ -24,6 +24,7 @@ from core.plugin.manager.exc import ( PluginDaemonNotFoundError, PluginDaemonUnauthorizedError, PluginInvokeError, + PluginNotFoundError, PluginPermissionDeniedError, PluginUniqueIdentifierError, ) @@ -143,7 +144,7 @@ class BasePluginManager: if rep.code != 0: try: error = PluginDaemonError(**json.loads(rep.message)) - except Exception as e: + except Exception: raise ValueError(f"{rep.message}, code: {rep.code}") self._handle_plugin_daemon_error(error.error_type, error.message) @@ -171,7 +172,7 @@ class BasePluginManager: try: line_data = json.loads(line) rep = PluginDaemonBasicResponse[type](**line_data) - except Exception as e: + except Exception: # TODO modify this when line_data has code and message if line_data and "error" in line_data: raise ValueError(line_data["error"]) @@ -182,7 +183,7 @@ class BasePluginManager: if rep.code == -500: try: error = PluginDaemonError(**json.loads(rep.message)) - except Exception as e: + except Exception: raise PluginDaemonInnerError(code=rep.code, message=rep.message) self._handle_plugin_daemon_error(error.error_type, error.message) @@ -226,6 +227,8 @@ class BasePluginManager: raise PluginDaemonNotFoundError(description=message) case PluginUniqueIdentifierError.__name__: raise PluginUniqueIdentifierError(description=message) + case PluginNotFoundError.__name__: + raise PluginNotFoundError(description=message) case PluginDaemonUnauthorizedError.__name__: raise PluginDaemonUnauthorizedError(description=message) case PluginPermissionDeniedError.__name__: diff --git a/api/services/agent_service.py b/api/services/agent_service.py index bb5a1892a4..3ee23f11a7 100644 --- a/api/services/agent_service.py +++ b/api/services/agent_service.py @@ -7,6 +7,7 @@ from flask_login import current_user # type: ignore import contexts from core.app.app_config.easy_ui_based_app.agent.manager import AgentConfigManager from core.plugin.manager.agent import PluginAgentManager +from core.plugin.manager.exc import PluginDaemonClientSideError from core.tools.tool_manager import ToolManager from extensions.ext_database import db from models.account import Account @@ -169,4 +170,7 @@ class AgentService: Get agent provider """ manager = PluginAgentManager() - return manager.fetch_agent_strategy_provider(tenant_id, provider_name) + try: + return manager.fetch_agent_strategy_provider(tenant_id, provider_name) + except PluginDaemonClientSideError as e: + raise ValueError(str(e)) from e