AssertCondition 政策

本頁內容適用於 ApigeeApigee Hybrid

查看 Apigee Edge 說明文件。

政策圖示

總覽

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

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

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

<AssertCondition>

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

條件陳述式的結果是布林值,可以是 truefalse
預設值 不適用
必填與否 必填
類型 複雜型別
父項元素 不適用
子元素 <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

This element has the following attributes that are common to all policies:

Attribute Default Required? Description
name N/A Required

The internal name of the policy. The value of the name attribute can contain letters, numbers, spaces, hyphens, underscores, and periods. This value cannot exceed 255 characters.

Optionally, use the <DisplayName> element to label the policy in the management UI proxy editor with a different, natural-language name.

continueOnError false Optional Set to false to return an error when a policy fails. This is expected behavior for most policies. Set to true to have flow execution continue even after a policy fails. See also:
enabled true Optional Set to true to enforce the policy. Set to false to turn off the policy. The policy will not be enforced even if it remains attached to a flow.
async   false Deprecated This attribute is deprecated.

子元素參照

本節說明 <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> 元素沒有屬性或子項元素。

錯誤代碼

本節說明這項政策觸發錯誤時,Apigee 傳回的錯誤代碼和錯誤訊息,以及 Apigee 設定的錯誤變數。如果您要開發錯誤處理規則,就必須瞭解這項資訊。如需更多資訊,請參閱「關於政策錯誤的相關資訊」和「處理錯誤」。

執行階段錯誤

政策執行時可能會發生這些錯誤。

錯誤代碼 HTTP 狀態 原因
steps.assertcondition.ConditionEvaluationFailed 500 無法評估條件陳述式。這類錯誤可能有多種原因,包括在執行期間變數中的值不正確。

部署錯誤

部署含有這項政策的 Proxy 時,可能會發生這些錯誤。

錯誤名稱 原因
InvalidCondition 政策無法驗證條件式陳述式。這類錯誤可能有多種原因,包括條件格式不正確,或使用不支援的運算子。

錯誤變數

每當政策發生執行錯誤時,Apigee 就會產生錯誤訊息。您可以在錯誤回應中查看這些錯誤訊息。很多時候,系統產生的錯誤訊息可能與產品情境無關。您可能會根據錯誤類型自訂錯誤訊息,讓訊息更有意義。

如要自訂錯誤訊息,您可以使用錯誤規則或 RaiseFault 政策。如要瞭解錯誤規則和 RaiseFault 政策的差異,請參閱 FaultRules 與 RaiseFault 政策。您必須在錯誤規則和 RaiseFault 政策中使用 Condition 元素檢查條件。Apigee 會提供每項政策專屬的錯誤變數,而錯誤變數的值會在政策觸發執行階段錯誤時設定。您可以使用這些變數,檢查特定錯誤情況並採取適當行動。如要進一步瞭解如何檢查錯誤條件,請參閱「建構條件」。

下表說明這項政策的特定錯誤變數。

變數 地點 範例
fault.name="FAULT_NAME" FAULT_NAME 是錯誤名稱,如執行階段錯誤表格所列。錯誤名稱是錯誤代碼的最後一個部分。 fault.name Matches "ConditionEvaluationFailed"
AssertCondition.POLICY_NAME.failed POLICY_NAME 是擲回錯誤的政策的使用者指定名稱。 AssertCondition.My-AssertCondition.failed = true
如要進一步瞭解政策錯誤,請參閱「關於政策錯誤的相關資訊」。