排解 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. 從錯誤回應的 errorstring 元素中找出 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 政策名稱,以及錯誤回應的 errorstring 元素失敗的原因。例如,在下列錯誤字串中,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 程式碼會執行以零為除數,該結果與修復字串中的「內容」相符:

    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 說明文件