fix: improve error handling in valid_reference_value function

This commit is contained in:
CaptainB 2025-07-11 17:57:01 +08:00
parent 7ec7984b9f
commit bf1062ff04
2 changed files with 52 additions and 43 deletions

View File

@ -20,8 +20,6 @@ from common.exception.app_exception import AppApiException
from common.utils.rsa_util import rsa_long_decrypt
from common.utils.tool_code import ToolExecutor
from maxkb.const import CONFIG
from system_manage.models import AuthTargetType
from system_manage.serializers.user_resource_permission import UserResourcePermissionSerializer
from tools.models import Tool
function_executor = ToolExecutor(CONFIG.get('SANDBOX'))
@ -48,23 +46,30 @@ def get_field_value(debug_field_list, name, is_required):
def valid_reference_value(_type, value, name):
if _type == 'int':
instance_type = int | float
elif _type == 'float':
instance_type = float | int
elif _type == 'dict':
instance_type = dict
elif _type == 'array':
instance_type = list
elif _type == 'string':
instance_type = str
else:
raise Exception(_('Field: {name} Type: {_type} Value: {value} Unsupported types').format(name=name,
_type=_type))
try:
if _type == 'int':
instance_type = int | float
elif _type == 'float':
instance_type = float | int
elif _type == 'dict':
value = json.loads(value) if isinstance(value, str) else value
instance_type = dict
elif _type == 'array':
value = json.loads(value) if isinstance(value, str) else value
instance_type = list
elif _type == 'string':
instance_type = str
else:
raise Exception(_(
'Field: {name} Type: {_type} Value: {value} Unsupported types'
).format(name=name, _type=_type))
except:
return value
if not isinstance(value, instance_type):
raise Exception(
_('Field: {name} Type: {_type} Value: {value} Type error').format(name=name, _type=_type,
value=value))
raise Exception(_(
'Field: {name} Type: {_type} Value: {value} Type error'
).format(name=name, _type=_type, value=value))
return value
def convert_value(name: str, value, _type, is_required, source, node):
@ -76,12 +81,8 @@ def convert_value(name: str, value, _type, is_required, source, node):
value = node.workflow_manage.get_reference_field(
value[0],
value[1:])
if isinstance(value, str):
try:
value = json.loads(value)
except:
pass
valid_reference_value(_type, value, name)
value = valid_reference_value(_type, value, name)
if _type == 'int':
return int(value)
if _type == 'float':

View File

@ -10,6 +10,8 @@ import json
import time
from typing import Dict
from django.utils.translation import gettext as _
from application.flow.i_step_node import NodeResult
from application.flow.step_node.tool_node.i_tool_node import IToolNode
from common.utils.tool_code import ToolExecutor
@ -30,20 +32,30 @@ def write_context(step_variable: Dict, global_variable: Dict, node, workflow):
def valid_reference_value(_type, value, name):
if _type == 'int':
instance_type = int | float
elif _type == 'float':
instance_type = float | int
elif _type == 'dict':
instance_type = dict
elif _type == 'array':
instance_type = list
elif _type == 'string':
instance_type = str
else:
raise Exception(500, f'字段:{name}类型:{_type} 不支持的类型')
try:
if _type == 'int':
instance_type = int | float
elif _type == 'float':
instance_type = float | int
elif _type == 'dict':
value = json.loads(value) if isinstance(value, str) else value
instance_type = dict
elif _type == 'array':
value = json.loads(value) if isinstance(value, str) else value
instance_type = list
elif _type == 'string':
instance_type = str
else:
raise Exception(_(
'Field: {name} Type: {_type} Value: {value} Unsupported types'
).format(name=name, _type=_type))
except:
return value
if not isinstance(value, instance_type):
raise Exception(f'字段:{name}类型:{_type}值:{value}类型错误')
raise Exception(_(
'Field: {name} Type: {_type} Value: {value} Type error'
).format(name=name, _type=_type, value=value))
return value
def convert_value(name: str, value, _type, is_required, source, node):
@ -53,12 +65,8 @@ def convert_value(name: str, value, _type, is_required, source, node):
value = node.workflow_manage.get_reference_field(
value[0],
value[1:])
if isinstance(value, str):
try:
value = json.loads(value)
except:
pass
valid_reference_value(_type, value, name)
value = valid_reference_value(_type, value, name)
if _type == 'int':
return int(value)
if _type == 'float':