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>

Use in addition to the name attribute to label the policy in the management UI proxy editor with a different, more natural-sounding name.

The <DisplayName> element is common to all policies.

Default Value N/A
Required? Optional. If you omit <DisplayName>, the value of the policy's name attribute is used.
Type String
Parent Element <PolicyElement>
Child Elements None

The <DisplayName> element uses the following syntax:

Syntax

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

Example

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

The <DisplayName> element has no attributes or child elements.

错误代码

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.