PythonScript 政策运行时错误问题排查

您正在查看的是 Apigee Edge 文档。
转到 Apigee X 文档
信息

ScriptEvaluationFailed

错误代码

steps.script.ScriptEvaluationFailed

错误响应正文

{
    "fault": {
        "faultstring": "Evaluation of script pythonscript_name (py) failed with reason: error_type: error_description"",
        "detail": {
            "errorcode": "steps.script.ScriptEvaluationFailed"
        }
    }
}

可能的原因

Python 脚本 政策可出现几种不同类型的 ScriptEvaluationFailed 错误。以下部分介绍了其中一些错误。

NameError

如果 PythonScript 代码中有一个变量未在定义时被引用或操作,则会出现名称错误。

错误响应正文

{
    "fault": {
        "faultstring": "Evaluation of script pythonscript_name (py) failed with reason: "NameError: variable_name is not defined"",
        "detail": {
            "errorcode": "steps.script.ScriptEvaluationFailed"
        }
    }
}

错误响应正文示例

{
    "fault": {
        "faultstring": "Evaluation of script myscript.py (py) failed with reason: "NameError: 'num3' is not defined"",
        "detail": {
            "errorcode": "steps.script.ScriptEvaluationFailed"
        }
    }
}

诊断

  1. 从错误响应的错误字符串元素中确定 PythonScript 政策和未定义的变量名称。例如,在以下故障字符串中,PythonScript 名称为 myscript.py,未定义的变量名称为 num3

    "faultstring": "Evaluation of script myscript.py (py) failed with reason: "NameError: 'num3' is not defined""
    
  2. 检查在上文第 1 步中确定的 PythonScript 源文件,并验证上述第 1 步中确定的未定义变量是否被引用。例如,以下 PythonScript 代码引用未定义变量 num3,其与错误字串符相匹配:

    num1 = 1.5
    num2 = 6.3
    sum = float(num1) + float(num3)
    print('The sum of {0} and {1} is {2}'.format(num1, num3 sum))
    
  3. 检查是否在 PythonScript 代码中定义了该特定变量。如果未定义该变量,则会导致此错误。

    在上面的示例脚本中,变量 num3 未定义。因此,您可能会遇到以下错误:

    "faultstring": "Evaluation of script myscript.py (py) failed with reason: "NameError: 'num3' is not defined""
    

分辨率

确保 PythonScript 代码中引用的所有变量均已正确定义。

要解决上面所示的 PythonScript 问题,请在使用变量之前定义变量 num3。例如:

num1 = 1.5
num2 = 6.3
num3 = 8.7
sum = float(num1) + float(num3)
print('The sum of {0} and {1} is {2}'.format(num1, num3, sum))

ZeroDivisionError

当除数或取模运算的第二个参数为零时,便会出现此错误。

错误响应正文


{
    "fault": {
        "faultstring": "Evaluation of script pythonscript_name (py) failed with reason: "ZeroDivisionError: reason_for_error"",
        "detail": {
            "errorcode": "steps.script.ScriptEvaluationFailed"
        }
    }
}

错误响应正文示例


{
    "fault": {
        "faultstring": "Evaluation of script myscript.py (py) failed with reason: "ZeroDivisionError: integer division or modulo by zero"",
        "detail": {
            "errorcode": "steps.script.ScriptEvaluationFailed"
        }
    }
}

诊断

  1. 从错误响应的错误字符串元素中确定 PythonScript 政策名称以及失败原因。例如,在以下故障字符串中,PythonScript 名称为 myscript.py,失败原因为 integer division or modulo by zero

    "faultstring": "Evaluation of script myscript.py (py) failed with reason: "ZeroDivisionError: integer division or modulo by zero""
    
  2. 检查在上述第 1 步中确定的 PythonScript 源文件,并验证除数法或取模运算是否为零。例如,以下 PythonScript 代码将按除数除以 0 执行,与故障字符串中的内容匹配:

    a = 0
    b = 5
    c = b/a
    print c
    

    在上述示例脚本中,因为除法运算的第二个参数为零,您会收到以下错误:

    "faultstring": "Evaluation of script myscript.py (py) failed with reason: "ZeroDivisionError: integer division or modulo by zero""
    

分辨率

确保 PythonScript 中的除法或取模运算的第二个参数为零。

要解决上面所示的 PythonScript 示例的问题,请使用非零值作为除法或取模运算的第二个参数。例如:

a = 3
b = 5
c = b/a
print c

更多信息

除了上述错误之外,还有许多其他原因可能会导致错误 steps.script.ScriptEvaluationFailed。如需了解详情,请参阅官方 Python 文档