Merge fix/chore-fix into fix/chore-fix

pull/9184/head
Yeuoly 2 years ago
parent 47c8824be6
commit 7b76b1ff82
No known key found for this signature in database
GPG Key ID: A66E7E320FB19F61

@ -87,3 +87,22 @@ class PluginVoiceEntity(BaseModel):
class PluginVoicesResponse(BaseModel): class PluginVoicesResponse(BaseModel):
voices: list[PluginVoiceEntity] = Field(description="The result of the voices.") voices: list[PluginVoiceEntity] = Field(description="The result of the voices.")
class PluginDaemonError(BaseModel):
"""
Error from plugin daemon.
"""
error_type: str
message: str
args: Optional[dict] = None
class PluginDaemonInnerError(Exception):
code: int
message: str
def __init__(self, code: int, message: str):
self.code = code
self.message = message

@ -1,13 +1,20 @@
import json import json
from collections.abc import Callable, Generator from collections.abc import Callable, Generator
from typing import TypeVar from typing import Optional, TypeVar
import requests import requests
from pydantic import BaseModel from pydantic import BaseModel
from yarl import URL from yarl import URL
from configs import dify_config from configs import dify_config
from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse from core.model_runtime.errors.invoke import (
InvokeAuthorizationError,
InvokeBadRequestError,
InvokeConnectionError,
InvokeRateLimitError,
InvokeServerUnavailableError,
)
from core.plugin.entities.plugin_daemon import PluginDaemonBasicResponse, PluginDaemonError, PluginDaemonInnerError
plugin_daemon_inner_api_baseurl = dify_config.PLUGIN_API_URL plugin_daemon_inner_api_baseurl = dify_config.PLUGIN_API_URL
plugin_daemon_inner_api_key = dify_config.PLUGIN_API_KEY plugin_daemon_inner_api_key = dify_config.PLUGIN_API_KEY
@ -110,6 +117,12 @@ class BasePluginManager:
rep = PluginDaemonBasicResponse[type](**json_response) rep = PluginDaemonBasicResponse[type](**json_response)
if rep.code != 0: if rep.code != 0:
if rep.code == -500:
try:
error = PluginDaemonError(**json.loads(rep.message))
self._handle_plugin_daemon_error(error.error_type, error.message, error.args)
except Exception as e:
raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}")
raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}") raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}")
if rep.data is None: if rep.data is None:
raise ValueError("got empty data from plugin daemon") raise ValueError("got empty data from plugin daemon")
@ -132,19 +145,34 @@ class BasePluginManager:
line_data = json.loads(line) line_data = json.loads(line)
rep = PluginDaemonBasicResponse[type](**line_data) rep = PluginDaemonBasicResponse[type](**line_data)
if rep.code != 0: if rep.code != 0:
raise PluginDaemonRespError(rep.message, rep.code) if rep.code == -500:
try:
error = PluginDaemonError(**json.loads(rep.message))
self._handle_plugin_daemon_error(error.error_type, error.message, error.args)
except Exception as e:
raise PluginDaemonInnerError(code=rep.code, message=rep.message)
raise ValueError(f"got error from plugin daemon: {rep.message}, code: {rep.code}")
if rep.data is None: if rep.data is None:
raise ValueError("got empty data from plugin daemon") raise ValueError("got empty data from plugin daemon")
yield rep.data yield rep.data
def _handle_plugin_daemon_error(self, error_type: str, message: str, args: Optional[dict] = None):
class PluginDaemonRespError(Exception): """
""" handle the error from plugin daemon
Plugin daemon response error. """
""" args = args or {}
def __init__(self, resp_message: str, code: int): if error_type == PluginDaemonInnerError.__name__:
super().__init__() raise PluginDaemonInnerError(code=-500, message=message)
self.message = f"got error from plugin daemon: {resp_message}, code: {code}" elif error_type == InvokeRateLimitError.__name__:
self.resp_message = resp_message raise InvokeRateLimitError(description=args.get("description"))
self.code = code elif error_type == InvokeAuthorizationError.__name__:
raise InvokeAuthorizationError(description=args.get("description"))
elif error_type == InvokeBadRequestError.__name__:
raise InvokeBadRequestError(description=args.get("description"))
elif error_type == InvokeConnectionError.__name__:
raise InvokeConnectionError(description=args.get("description"))
elif error_type == InvokeServerUnavailableError.__name__:
raise InvokeServerUnavailableError(description=args.get("description"))
else:
raise ValueError(f"got unknown error from plugin daemon: {error_type}, message: {message}, args: {args}")

@ -618,9 +618,6 @@ class ToolManager:
""" """
get api provider get api provider
""" """
"""
get tool provider
"""
provider_obj: ApiToolProvider | None = ( provider_obj: ApiToolProvider | None = (
db.session.query(ApiToolProvider) db.session.query(ApiToolProvider)
.filter( .filter(

Loading…
Cancel
Save