AssertCondition 政策

本頁適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

政策圖示

總覽

AssertCondition 政策會在要求或回應流程中的執行階段評估條件式陳述式。您可以根據流程變數定義條件,並使用這項政策斷言條件。條件一律會評估為布林值,也就是 true 或 false。如要進一步瞭解如何編寫條件式陳述式,請參閱「條件參考資料」。

評估條件後,AssertCondition 政策會將評估結果儲存在 assertcondition.policy-name.truthValue 流程變數中。您可以在後續摘要或協調邏輯中使用產生的流程變數。如果條件評估為 true,變數的值會設為 true,否則為 false。如果您定義了多個 AssertCondition 政策,變數名稱中的 policy-name 可協助您唯一識別變數。

這項政策是標準政策,可部署至任何環境類型。如要瞭解政策類型和各環境類型的可用性,請參閱「政策類型」。

<AssertCondition>

定義 <AssertCondition> 政策。您可以使用這項政策評估條件陳述式,其中包含一或多個由邏輯運算子連結的條件。如要瞭解條件中支援的所有運算子,請參閱「運算子」。

條件陳述式的結果是布林值,可以是 truefalse
預設值 不適用
是否必要? 必填
類型 複雜類型
上層元素 N/A
子元素 <Condition>
<DisplayName>

下表概略說明 <AssertCondition> 的子元素:

子元素 是否必要 說明
<Condition> 指定要評估的條件。
<DisplayName> 選用 政策的自訂名稱。

<AssertCondition> 元素使用以下語法:

語法

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssertCondition">
    <!-- Display name for this policy -->
    <DisplayName>DISPLAY_NAME</DisplayName>
    <!-- Assertion's condition where operators are defined -->
    <Condition>CONDITIONAL_STATEMENT</Condition>
</AssertCondition>

範例

以下範例會檢查 google.dialogflow.my-prefix.claimAmount 變數是否大於 0 且小於 1000。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssertCondition continueOnError="false" enabled="true"
        name="MyAssertCondition">
    <DisplayName>Assert My Condition</DisplayName>
    <Condition>(google.dialogflow.my-prefix.claimAmount > 0)
                and
               (google.dialogflow.my-prefix.claimAmount LesserThan 1000)</Condition>
</AssertCondition>

在這個例子中:

  • 如果 google.dialogflow.my-prefix.claimAmount 變數的值為 500,則條件會評估為 true,因此 assertcondition.MyAssertCondition.truthValue 變數會設為 true
  • 不過,如果 google.dialogflow.my-prefix.claimAmount 變數的值為 1200,assertcondition.MyAssertCondition.truthValue 變數會設為 false

這個元素包含下列所有政策都適用的屬性:

屬性 預設 是否必要? 說明
name 不適用 必要

政策的內部名稱。name 屬性的值可以包含英文字母、數字、空格、連字號、底線和句號。這個值不得超過 255 個半形字元。

您可以選擇使用 <DisplayName> 元素,在管理 UI 代理程式編輯器中為政策加上不同、自然語言的名稱。

continueOnError false 選用 將其設為 false,即可在政策失敗時傳回錯誤。這是大多數政策的預期行為。將其設為 true,即使政策失敗,流程執行作業仍會繼續進行。另請參閱:
enabled 選用 設為 true 即可強制執行政策。設為 false 即可關閉政策。即使政策仍附加至流程,系統也不會強制執行這項政策。
async   false 已淘汰 此屬性已淘汰。

子元素參照

本節將說明 <AssertCondition> 的子元素。

<Condition>

指定要評估的條件。如要進一步瞭解如何在 Apigee 中編寫條件式陳述式,請參閱「條件參考資料」。

預設值 不適用
是否必要? 必填
類型 字串
上層元素 <AssertCondition>
子元素

<DisplayName>

除了 name 屬性之外,您也可以在管理 UI 代理程式編輯器中使用其他更自然的名稱標記政策。

<DisplayName> 元素適用於所有政策。

預設值 不適用
是否必要? (非必要) 如果省略 <DisplayName>,系統會使用政策的 name 屬性值。
類型 字串
上層元素 <PolicyElement>
子元素

<DisplayName> 元素使用以下語法:

語法

<PolicyElement>
  <DisplayName>POLICY_DISPLAY_NAME</DisplayName>
  ...
</PolicyElement>

範例

<PolicyElement>
  <DisplayName>My Validation Policy</DisplayName>
</PolicyElement>

<DisplayName> 元素沒有屬性或子項元素。

錯誤代碼

This section describes the fault codes and error messages that are returned and fault variables that are set by Apigee when this policy triggers an error. This information is important to know if you are developing fault rules to handle faults. To learn more, see What you need to know about policy errors and Handling faults.

Runtime errors

These errors can occur when the policy executes.

Fault code HTTP status Cause
steps.assertcondition.ConditionEvaluationFailed 500 Failed to evaluate the conditional statement. There can be many reasons for this error, including incorrect values in the variables at run time.

Deployment errors

These errors can occur when you deploy a proxy containing this policy.

Error name Cause
InvalidCondition The policy was not able to validate the conditional statement. There can be many reasons for this error, including malformed conditions or use of unsupported operators.

Fault variables

Whenever there are execution errors in a policy, Apigee generates error messages. You can view these error messages in the error response. Many a time, system generated error messages might not be relevant in the context of your product. You might want to customize the error messages based on the type of error to make the messages more meaningful.

To customize the error messages, you can use either fault rules or the RaiseFault policy. For information about differences between fault rules and the RaiseFault policy, see FaultRules vs. the RaiseFault policy. You must check for conditions using the Condition element in both the fault rules and the RaiseFault policy. Apigee provides fault variables unique to each policy and the values of the fault variables are set when a policy triggers runtime errors. By using these variables, you can check for specific error conditions and take appropriate actions. For more information about checking error conditions, see Building conditions.

The following table describes the fault variables specific to this policy.

Variables Where Example
fault.name="FAULT_NAME" FAULT_NAME is the name of the fault, as listed in the Runtime errors table. The fault name is the last part of the fault code. fault.name Matches "ConditionEvaluationFailed"
AssertCondition.POLICY_NAME.failed POLICY_NAME is the user-specified name of the policy that threw the fault. AssertCondition.My-AssertCondition.failed = true
For more information about policy errors, see What you need to know about policy errors.