Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(tools): translate PluginNotFoundError to ToolProviderNotFoundError (#41837)

Taranum Wasu486bcb0b08ee
diff --git a/api/core/tools/tool_manager.py b/api/core/tools/tool_manager.py
index e928386e502..1fce4931bf3 100644
--- a/api/core/tools/tool_manager.py
+++ b/api/core/tools/tool_manager.py
@@ -22,6 +22,7 @@ from core.entities import PluginCredentialType
from core.helper.module_import_helper import load_single_subclass_from_source
from core.helper.position_helper import is_filtered
from core.helper.provider_cache import ToolProviderCredentialsCache
+from core.plugin.impl.exc import PluginDaemonNotFoundError, PluginNotFoundError
from core.plugin.impl.tool import PluginToolManager
from core.tools.__base.tool import Tool
from core.tools.__base.tool_provider import ToolProviderController
@@ -161,7 +162,14 @@ class ToolManager:
return plugin_tool_providers[provider]
manager = PluginToolManager()
- provider_entity = manager.fetch_tool_provider(tenant_id, provider)
+ try:
+ provider_entity = manager.fetch_tool_provider(tenant_id, provider)
+ except (PluginNotFoundError, PluginDaemonNotFoundError) as exc:
+ # The plugin daemon rejected the provider name (probably an
+ # unknown or non-builtin identifier). Translate to the
+ # console's domain error so the API returns a 4xx instead
+ # of leaking the daemon's ``PluginNotFoundError`` as a 500.
+ raise ToolProviderNotFoundError(f"plugin provider {provider} not found") from exc
if not provider_entity:
raise ToolProviderNotFoundError(f"plugin provider {provider} not found")
diff --git a/api/tests/unit_tests/core/tools/test_tool_manager.py b/api/tests/unit_tests/core/tools/test_tool_manager.py
index 515a9d2dacc..a25e69f2db8 100644
--- a/api/tests/unit_tests/core/tools/test_tool_manager.py
+++ b/api/tests/unit_tests/core/tools/test_tool_manager.py
@@ -18,6 +18,7 @@ from sqlalchemy.orm import Session
from core.app.entities.app_invoke_entities import InvokeFrom
from core.plugin.entities.plugin_daemon import CredentialType
+from core.plugin.impl.exc import PluginDaemonNotFoundError, PluginNotFoundError
from core.tools.__base.tool_runtime import ToolRuntime
from core.tools.entities.tool_entities import (
ApiProviderAuthType,
@@ -300,6 +301,39 @@ def test_get_plugin_provider_raises_when_provider_missing():
ToolManager.get_plugin_provider("provider-a", "tenant-1")
+ "daemon_error",
+ [
+ # The plugin daemon's typed "plugin not found" — happens when the
+ # builtin credential endpoint is hit with a provider name that
+ # neither the builtin nor the plugin manager owns.
+ PluginNotFoundError("plugin not found"),
+ # The lower-level daemon unreachable variant.
+ PluginDaemonNotFoundError("daemon not reachable"),
+ ],
+)
+def test_get_plugin_provider_translates_plugin_not_found_to_domain_error(daemon_error):
+ """#41805: ``ToolManager.get_builtin_provider`` falls back to
+ ``PluginToolManager.fetch_tool_provider``. The pre-fix code let the
+ plugin daemon's ``PluginNotFoundError`` bubble up to the console API
+ generic exception handler as a 500. Translate it to
+ ``ToolProviderNotFoundError`` at this boundary so the API returns a
+ controlled 4xx, consistent with the missing-provider path the
+ existing ``test_get_plugin_provider_raises_when_provider_missing``
+ test already covers.
+ """
+ provider_context = _SimpleContextVar()
+ lock_context = _SimpleContextVar()
+ lock_context.set(threading.Lock())
+
+ with patch("core.tools.tool_manager.contexts.plugin_tool_providers", provider_context):
+ with patch("core.tools.tool_manager.contexts.plugin_tool_providers_lock", lock_context):
+ with patch("core.tools.tool_manager.PluginToolManager") as mock_manager_cls:
+ mock_manager_cls.return_value.fetch_tool_provider.side_effect = daemon_error
+ with pytest.raises(ToolProviderNotFoundError, match="plugin provider provider-a not found"):
+ ToolManager.get_plugin_provider("provider-a", "tenant-1")
+
+
def test_get_tool_runtime_builtin_without_credentials():
tool = Mock()
tool.fork_tool_runtime.return_value = "runtime-tool"