Bläddra i källkod

chore: enhance error message when handling PluginInvokeError (#22908)

tags/1.7.1
Yeuoly 3 månader sedan
förälder
incheckning
206bc4b36d
Inget konto är kopplat till bidragsgivarens mejladress

+ 20
- 0
api/core/plugin/impl/exc.py Visa fil

from collections.abc import Mapping

from pydantic import TypeAdapter


class PluginDaemonError(Exception): class PluginDaemonError(Exception):
"""Base class for all plugin daemon errors.""" """Base class for all plugin daemon errors."""


class PluginInvokeError(PluginDaemonClientSideError): class PluginInvokeError(PluginDaemonClientSideError):
description: str = "Invoke Error" description: str = "Invoke Error"


def _get_error_object(self) -> Mapping:
try:
return TypeAdapter(Mapping).validate_json(self.description)
except Exception:
return {}

def get_error_type(self) -> str:
return self._get_error_object().get("error_type", "unknown")

def get_error_message(self) -> str:
try:
return self._get_error_object().get("message", "unknown")
except Exception:
return self.description



class PluginUniqueIdentifierError(PluginDaemonClientSideError): class PluginUniqueIdentifierError(PluginDaemonClientSideError):
description: str = "Unique Identifier Error" description: str = "Unique Identifier Error"

+ 26
- 3
api/core/workflow/nodes/tool/tool_node.py Visa fil



from core.callback_handler.workflow_tool_callback_handler import DifyWorkflowCallbackHandler from core.callback_handler.workflow_tool_callback_handler import DifyWorkflowCallbackHandler
from core.file import File, FileTransferMethod from core.file import File, FileTransferMethod
from core.plugin.impl.exc import PluginDaemonClientSideError
from core.plugin.impl.exc import PluginDaemonClientSideError, PluginInvokeError
from core.plugin.impl.plugin import PluginInstaller from core.plugin.impl.plugin import PluginInstaller
from core.tools.entities.tool_entities import ToolInvokeMessage, ToolParameter from core.tools.entities.tool_entities import ToolInvokeMessage, ToolParameter
from core.tools.errors import ToolInvokeError from core.tools.errors import ToolInvokeError
tenant_id=self.tenant_id, tenant_id=self.tenant_id,
node_id=self.node_id, node_id=self.node_id,
) )
except (PluginDaemonClientSideError, ToolInvokeError) as e:
except ToolInvokeError as e:
yield RunCompletedEvent( yield RunCompletedEvent(
run_result=NodeRunResult( run_result=NodeRunResult(
status=WorkflowNodeExecutionStatus.FAILED, status=WorkflowNodeExecutionStatus.FAILED,
inputs=parameters_for_log, inputs=parameters_for_log,
metadata={WorkflowNodeExecutionMetadataKey.TOOL_INFO: tool_info}, metadata={WorkflowNodeExecutionMetadataKey.TOOL_INFO: tool_info},
error=f"Failed to transform tool message: {str(e)}",
error=f"Failed to invoke tool {node_data.provider_name}: {str(e)}",
error_type=type(e).__name__,
)
)
except PluginInvokeError as e:
yield RunCompletedEvent(
run_result=NodeRunResult(
status=WorkflowNodeExecutionStatus.FAILED,
inputs=parameters_for_log,
metadata={WorkflowNodeExecutionMetadataKey.TOOL_INFO: tool_info},
error="An error occurred in the plugin, "
f"please contact the author of {node_data.provider_name} for help, "
f"error type: {e.get_error_type()}, "
f"error details: {e.get_error_message()}",
error_type=type(e).__name__,
)
)
except PluginDaemonClientSideError as e:
yield RunCompletedEvent(
run_result=NodeRunResult(
status=WorkflowNodeExecutionStatus.FAILED,
inputs=parameters_for_log,
metadata={WorkflowNodeExecutionMetadataKey.TOOL_INFO: tool_info},
error=f"Failed to invoke tool, error: {e.description}",
error_type=type(e).__name__, error_type=type(e).__name__,
) )
) )

+ 1
- 1
api/tests/unit_tests/core/workflow/nodes/tool/test_tool_node.py Visa fil

assert isinstance(result, NodeRunResult) assert isinstance(result, NodeRunResult)
assert result.status == WorkflowNodeExecutionStatus.FAILED assert result.status == WorkflowNodeExecutionStatus.FAILED
assert "oops" in result.error assert "oops" in result.error
assert "Failed to transform tool message:" in result.error
assert "Failed to invoke tool" in result.error
assert result.error_type == "ToolInvokeError" assert result.error_type == "ToolInvokeError"

Laddar…
Avbryt
Spara