Kebijakan AssertCondition

Halaman ini berlaku untuk Apigee dan Apigee hybrid.

Lihat dokumentasi Apigee Edge.

Kebijakan AssertCondition mengevaluasi pernyataan kondisional saat runtime dalam alur permintaan atau respons. Anda dapat menentukan kondisi berdasarkan variabel alur, dan menggunakan kebijakan ini untuk menegaskan kondisi tersebut. Kondisi selalu dievaluasi ke nilai boolean, baik benar atau salah. Untuk mengetahui informasi selengkapnya tentang cara menulis pernyataan bersyarat, lihat Referensi kondisi.

Setelah mengevaluasi kondisi, kebijakan AssertCondition menyimpan hasil evaluasi dalam variabel alur assertcondition.policy-name.truthValue. Anda dapat menggunakan variabel alur yang dihasilkan dalam info atau logika teratur berikutnya. Jika kondisi dievaluasi sebagai benar, nilai variabel ditetapkan ke true, false jika tidak. Jika Anda telah menentukan beberapa kebijakan AssertCondition, policy-name dalam nama variabel akan membantu Anda mengidentifikasi variabel secara unik.

Kebijakan ini adalah Kebijakan standar dan dapat di-deploy ke jenis lingkungan apa pun. Untuk mengetahui informasi tentang jenis dan ketersediaan kebijakan dengan setiap jenis lingkungan, lihat Jenis kebijakan.

<AssertCondition>

Menentukan kebijakan <AssertCondition>. Dengan menggunakan kebijakan ini, Anda dapat mengevaluasi pernyataan bersyarat yang memiliki satu atau beberapa kondisi yang digabungkan oleh operator logika. Untuk mengetahui informasi tentang semua operator yang didukung dalam kondisi, lihat Operator.

Hasil pernyataan kondisional adalah boolean yang dapat berupa true atau false.
Nilai Default T/A
Wajib? Wajib
Jenis Jenis kompleks
Elemen Induk T/A
Elemen Turunan <Condition>
<DisplayName>

Tabel berikut memberikan deskripsi umum tentang elemen turunan <AssertCondition>:

Elemen Turunan Wajib? Deskripsi
<Condition> Ya Menentukan kondisi yang akan dievaluasi.
<DisplayName> Opsional Nama kustom untuk kebijakan.

Elemen <AssertCondition> menggunakan sintaksis berikut:

Sintaksis

<?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>

Contoh

Contoh berikut memeriksa apakah variabel google.dialogflow.my-prefix.claimAmount lebih besar dari 0 dan kurang dari 1.000.

<?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>

Dalam contoh ini:

  • Jika nilai variabel google.dialogflow.my-prefix.claimAmount adalah 500, kondisi dievaluasi sebagai benar sehingga variabel assertcondition.MyAssertCondition.truthValue ditetapkan ke true.
  • Namun, jika nilai variabel google.dialogflow.my-prefix.claimAmount adalah 1200, variabel assertcondition.MyAssertCondition.truthValue akan ditetapkan ke false.

Elemen ini memiliki atribut berikut yang umum untuk semua kebijakan:

Atribut Default Wajib? Deskripsi
name T/A Wajib

Nama internal kebijakan. Nilai atribut name dapat berisi huruf, angka, spasi, tanda hubung, garis bawah, dan titik. Nilai ini tidak boleh melebihi 255 karakter.

Secara opsional, gunakan elemen <DisplayName> untuk memberi label pada kebijakan di editor proxy UI pengelolaan dengan nama bahasa alami yang berbeda.

continueOnError false Opsional Tetapkan ke false untuk menampilkan error saat kebijakan gagal. Perilaku ini wajar terjadi untuk sebagian besar kebijakan. Tetapkan ke true agar eksekusi alur berlanjut meskipun setelah kebijakan gagal. Lihat juga:
enabled benar Opsional Tetapkan ke true untuk menerapkan kebijakan. Tetapkan ke false untuk menonaktifkan kebijakan. Kebijakan tidak akan diterapkan meskipun tetap terlampir ke alur.
async   false Tidak digunakan lagi Atribut ini tidak digunakan lagi.

Referensi elemen turunan

Bagian ini menjelaskan elemen turunan <AssertCondition>.

<Condition>

Menentukan kondisi yang akan dievaluasi. Untuk mengetahui informasi selengkapnya tentang cara menulis pernyataan bersyarat di Apigee, lihat Referensi kondisi.

Nilai Default T/A
Wajib? Wajib
Jenis String
Elemen Induk <AssertCondition>
Elemen Turunan Tidak ada

<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.

Kode error

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.