SOAP 訊息驗證政策執行階段錯誤疑難排解

查看 Apigee Edge 說明文件。
前往 Apigee X說明文件
資訊

SourceMessageNotAvailable

錯誤代碼

steps.messagevalidation.SourceMessageNotAvailable

錯誤回應主體

{
  "fault": {
    "faultstring": "source_var_name message is not
     available for[policy_name]",
    "detail": {
      "Errorcode":
       "steps.messagevalidation.SourceMessageNotAvailable"
     }
  }
}

錯誤回應主體範例

{
  "fault": {
    "faultstring": "request message is not available for MessageValidation: SOAP-Message-Validation-1",
    "detail": {
      "errorcode": "steps.messagevalidation.SourceMessageNotAvailable"
    }
  }
}

原因

如果 SOAPMessageValidation 政策的 <Source> 元素中指定的 message 變數符合以下兩者之一,就會發生這個錯誤:

  • 範圍外 (不適用於執行政策的特定流程)
  • 無法解析 (未定義)

舉例來說,如果 SOAPMessageValidation 政策中的 <Source> 元素設為變數不存在,執行政策的流程中就會發生這個錯誤。

診斷

  1. 找出發生錯誤的 SOAPMessageValidation 政策名稱,以及錯誤字串中的 source 變數名稱。舉例來說,在下列錯誤字串中,SOAPMessageValidation 政策名稱為 SOAP-Message-Validation-1source 變數為 request

    "faultstring": "request message is not available for MessageValidation: SOAP-Message-Validation-1"
    
  2. 在失敗的 SOAPMessageValidation 政策 XML 中,確認 <Source> 元素中設定的變數名稱與錯誤字串 (上述步驟 1) 中識別的變數名稱相符。

    舉例來說,下列 SOAPMessageValidation 政策會在 <Source> 元素中指定名為 request 的變數,與錯誤字串中的內容相符:

    <MessageValidation async="false" continueOnError="false" enabled="true" name="SOAP-Message-Validation-1">
        <DisplayName>SOAP Message Validation-1</DisplayName>
        <Properties/>
        <Element namespace="http://schemas.xmlsoap.org/soap/prices">GetPriceResponse</Element>
        <SOAPMessage/>
        <Source>request</Source>
        <ResourceURL>xsd://Script-1.xsd</ResourceURL>
    </MessageValidation>
    
  3. 判斷 <Source> 元素中使用的變數是否已定義,且在執行 SOAPMessageValidation 政策的流程中可用。

    如果變數為以下任一變數:

    • 超出範圍 (在執行政策的特定流程中不可用),或
    • 無法解析 (未定義)

    這就是發生錯誤的原因

    舉例來說,假設上方顯示的 SOAPMessageValidation 政策是在回應流程中執行。不過,上述 <Source> 元素範例中使用的 request 變數僅適用於 request 流程。

    由於回應流程中沒有 request 變數,您會收到下列錯誤:

    "faultstring": "request message is not available for MessageValidation: SOAP-Message-Validation-1"
    

解析度

請確認已定義失敗 SOAPMessageValidation 政策的 <Source> 元素中設定的變數,並存在於執行政策的流程中。

如要修正上述範例,您可以修改 <Source>要使用的元素 response 變數,存在於回應流程中:

<MessageValidation async="false" continueOnError="false" enabled="true" name="SOAP-Message-Validation-1">
    <DisplayName>SOAP Message Validation-1</DisplayName>
    <Properties/>
    <Element namespace="http://schemas.xmlsoap.org/soap/prices">GetPriceResponse</Element>
    <SOAPMessage/>
    <Source>response</Source>
    <ResourceURL>xsd://Script-1.xsd</ResourceURL>
</MessageValidation>

NonMessageVariable

錯誤代碼

steps.messagevalidation.NonMessageVariable

錯誤回應主體

{
  "fault": {
    "faultstring": "Variable var_name does not resolve to a Message"
    "detail": {
      "errorcode": "steps.messagevalidation.NonMessageVariable"
    }
  }
}

錯誤回應主體範例

{
  "fault": {
    "faultstring": "Variable message.content does not resolve to a Message",
    "detail": {
      "errorcode": "steps.messagevalidation.NonMessageVariable"
    }
  }
}

原因

如果 SOAPMessageValidation 政策中的 <Source> 元素設為非 Message 類型的變數,就會發生這個錯誤。

訊息類型變數代表整個 HTTP 要求和回應。內建的 Apigee Edge 流程變數 requestresponsemessage 是 Message 類型。如要進一步瞭解訊息變數,請參閱變數參考資料

診斷

  1. 找出無法從錯誤字串解析為 Message 類型的變數名稱。例如,在以下錯誤字串中,變數名稱為 message.content

    "faultstring": "Variable message.content does not resolve to a Message"
    
  2. 檢查失敗的特定 API Proxy 中,所有 SOAPMessageValidation 政策。可能會有一或多項 SOAPMessageValidation 政策。找出特定的 SOAPMessageValidation 政策,其中 <Source> 元素中指定的變數與錯誤字串中指定的變數名稱相符 (請參閱上述步驟 1)。

    舉例來說,下列政策會將 <Source> 元素設為名為 message.content 的變數,此變數與錯誤字串中的內容相符:

    <MessageValidation async="false" continueOnError="false" enabled="true" name="SOAP-Message-Validation-1">
        <DisplayName>SOAP Message Validation-1</DisplayName>
        <Properties/>
        <Element namespace="http://schemas.xmlsoap.org/soap/prices">GetPriceResponse</Element>
        <SOAPMessage/>
        <Source>message.content</Source>
        <ResourceURL>xsd://Script-1.xsd</ResourceURL>
    </MessageValidation>
    

    由於 message.content 變數並非「Message」類型,因此您會收到錯誤訊息:

    "faultstring": "Variable message.content does not resolve to a Message"
    

解析度

確認失敗 SOAPMessageValidation 政策中的 <Source> 元素已設為執行政策執行流程中的 Message 類型變數。

如要修正政策,您可以修改 <Source> 元素,指定「Message」類型的變數。例如,在失敗的 SOAPMessageValidation 政策中, 您可以將 <Source> 元素指定為 request

<MessageValidation async="false" continueOnError="false" enabled="true" name="SOAP-Message-Validation-1">
    <DisplayName>SOAP Message Validation-1</DisplayName>
    <Properties/>
    <Element namespace="http://sample.com"> sampleObject</Element>
    <SOAPMessage/>
    <Source>request</Source>
    <ResourceURL>xsd://Script-1.xsd</ResourceURL>
</MessageValidation>

失敗

錯誤代碼

steps.messagevalidation.Failed

錯誤回應主體

{
  "fault": {
    "faultstring": "Variable var_name failed with reason: \"reason [Line varline_num
    "detail": {
      "errorcode": "steps.messagevalidation.Failed"
    }
  }
}

錯誤回應主體範例

{
  "fault": {
    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Expecting a child element but found none [Line 9]\"",
    "detail": {
      "errorcode": "steps.messagevalidation.Failed"
    }
  }
}

原因

如果 SOAPMessageValidation 政策無法根據 XSD 結構定義或 WSDL 定義來驗證輸入訊息酬載,就會發生這個錯誤。如果酬載訊息中含有格式不正確的 JSON 或 XML,也會發生這個問題。

以下是可能導致這項錯誤的原因:

原因 說明
不相符的 XML 酬載 輸入 XML 酬載不符合 SoapMessageValidation 政策中指定的 XSD 結構定義。
SOAP 酬載不符 輸入 SOAP 酬載不符合 SoapMessageValidation 政策中指定的 WSDL 定義。
JSON 或 XML 格式錯誤 輸入 SOAP 酬載缺少格式正確的 XML 或 JSON。

原因:不相符的 XML 酬載

如果輸入的 XML 酬載不符合 XSD 結構定義,就會發生這個錯誤 在 SoapMessageValidation 政策的 <ResourceURL> 元素中指定。

錯誤訊息範例

{
  "fault": {
    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Expecting a child element but found none [Line 9]\"",
    "detail": {
      "errorcode": "steps.messagevalidation.Failed"
    }
  }
}

診斷

  1. 找出 SOAPMessageValidation 政策名稱、失敗原因,以及 XML 酬載與 XSD 結構定義不符的行號。這些資訊都位於錯誤字串中。舉例來說,在下列錯誤字串中,SOAPMessageValidation 政策名稱為SOAP-Message-Validation-1,失敗原因為 Expecting a child element but found none [Line 9]:,行號為 9

    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Expecting a child element but found none [Line 9]\""
    
  2. 檢查 SOAPMessageValidation 政策,並確認政策使用 XSD 結構定義檔案來驗證訊息。舉例來說,下列 SOAPMessageValidation 政策的 <ResourceURL> 元素中包含 XSD 資源檔案:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <MessageValidation async="false" continueOnError="false" enabled="true" name="SOAP-Message-Validation-1">
        <DisplayName>SOAP Message Validation-1</DisplayName>
        <Properties/>
        <SOAPMessage/>
        <Source>request</Source>
        <ResourceURL>xsd://Script-1.xsd</ResourceURL>
    </MessageValidation>
    
  3. 檢查您在上方步驟 1 找到的行數的輸入酬載,瞭解失敗的原因。

    輸入 XML 酬載範例

    <?xml version="1.0"?>
    <soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
    <soap:Body>
        <shipOrder
        xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
        <shipTo>
            <name>Karl Kalckstein</name>
        </shipTo>
        <items>
            <item>
                <title>I love APIs</title>
                <quantity>1</quantity>
                <price>2.90</price>
            </item>
        </items>
    </shipOrder>
    </soap:Body>
    </soap:Envelope>
    

    在上述範例中,第 9 行有 </shipTo> 結束標記。

  4. 檢查 SOAPMessageValidation 政策中使用的 XML 結構定義定義檔案 Script-1.xsd,瞭解 <shipTo> 元素應如何呈現:

    ...
    <xsd:element name="shipTo" type="shipAddress"/>
    ...
    <xsd:complexType name="shipAddress">
    <xsd:sequence>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="street" type="xsd:string"/>
    <xsd:element name="address" type="xsd:string"/>
    <xsd:element name="country" type="xsd:string"/>
    </xsd:sequence>
    </xsd:complexType>
    ...
    

    根據用於驗證的 XSD 範例,<shipTo> 元素必須包含子元素 <name><street><address><country>。不過,輸入 XML 酬載只有一個子元素 <name>。因此會收到下列錯誤訊息:

    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Expecting a child element but found none [Line 9]\""
    

解析度

有兩種方式可以解決此驗證錯誤:

解決方案 #1

如果您判定 XSD 結構定義包含不必要的限制,且 <shipTo> 元素只需要使用 <name> 子元素,您可以修改 SOAPMessageValidation 政策中使用的 Script-1.xsd。如要驗證上述酬載範例,您可以修改 XSD 檔案,如下所示:

...
<xsd:element name="shipTo" type="shipAddress"/>
...
<xsd:complexType name="shipAddress">
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>

解決方案 #2

如果 XSD 定義檔案正確無誤,則可以變更輸入的 XML 酬載以符合 xsd。舉例來說,您可以修改酬載,使其符合 xsd,如下所示:

<?xml version="1.0">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
    <shipOrder xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
    <shipTo>
        <name>Karl Kalckstein</name>
        <street>1020 Parkway</street>
        <address>Mountain View, CA 94043</address>
        <country>USA</country>
    </shipTo>
    <items>
        <item>
            <title>I love APIs</title>
            <quantity>1</quantity>
            <price>2.90</price>
        </item>
    </items>
</shipOrder>
</soap:Body>
</soap:Envelope>

原因:不相符的 SOAP 酬載

如果輸入的 SOAP 酬載不符合 SoapMessageValidation 政策的 <ResourceURL> 元素中指定的 WSDL 定義,就會發生這項錯誤。

錯誤訊息範例

{
  "fault": {
    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Element name mismatch. Wildcard? [Line 11]\"",
    "detail": {
      "errorcode": "steps.messagevalidation.Failed"
    }
  }
}

診斷

  1. 找出 SOAPMessageValidation 政策名稱、失敗原因,以及 SOAP 酬載與 WSDL 定義不符的行號。您可以在錯誤字串中找到上述所有資訊。舉例來說,在下列錯誤字串中,SOAPMessageValidation 政策名稱為 SOAP-Message-Validation-1,失敗的原因為 \"Element name mismatch. Wildcard? [Line 11]\"",行號為 11

    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Element name mismatch. Wildcard? [Line 11]\""
    
  2. 檢查 SOAPMessageValidation 政策,並確認政策使用 wsdl 結構定義來驗證訊息。舉例來說,下列 SOAPMessageValidation 政策的 <ResourceURL> 元素中有 wsdl 資源檔案:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <MessageValidation async="false" continueOnError="false" enabled="true" name="SOAP-Message-Validation-1">
        <DisplayName>SOAP Message Validation-1</DisplayName>
        <Properties/>
        <SOAPMessage/>
        <Source>request</Source>
     <ResourceURL>wsdl://SOAP-Message-Validation-1.wsdl</ResourceURL>
    </MessageValidation>
    
  3. 檢查步驟 1 找出的行數的輸入酬載,瞭解導致失敗的原因。

    輸入 SOAP 酬載範例

    <?xml version="1.0"?>
    <soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
    <soap:Body>
    <shipOrder
    xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
      <shipTo>
        <name>Karl Kalckstein</name>
        <street>1020 Parkway</street>
        <address>Mountain View, CA 94043</address>
        <country>USA</country>
      </shipTo>
      <items>
        <item>
          <title>I love APIs</title>
          <quantity>1</quantity>
          <price>2.90</price>
        </item>
      </items>
    

    在上述範例中,第 11 行有 <shipTo> 元素下的子元素 <country>

  4. 請檢查 SOAPMessageValidation 政策中使用的 WSDL 定義檔案 SOAP-Message-Validation-1.wsdl,瞭解 <shipTo> 元素下子元素 <country> 可能有何問題:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
        <wsdl:types>
          <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="shipOrder" type="order"/>
    <xsd:complexType name="order">
    <xsd:sequence>
    <xsd:element name="shipTo" type="shipAddress"/>
    <xsd:element name="items" type="cdItems"/>
    </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="shipAddress">
    <xsd:sequence>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="street" type="xsd:string"/>
    <xsd:element name="address" type="xsd:string"/>
    </xsd:sequence>
    ...
    

    根據用於驗證的 WSDL 定義,<shipTo> 元素不含 <country> 子元素。因此會收到下列錯誤訊息:

    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Element name mismatch. Wildcard? [Line 11]\""
    

解析度

有兩種方式可以解決此驗證錯誤:

解決方案 #1

如果您發現使用的 WSDL 定義有誤,可以變更 SOAPMessageValidation 政策中使用的 SOAP-Message-Validation-1.wsdl。如要驗證上述酬載範例,您可以修改檔案,如下所示:

...
<xsd:complexType name="shipAddress">`
...
<sequence>
    <element name="name" type="xsd:string"/>
    <element name="street" type="xsd:string"/>
    <element name="address" type="xsd:string"/>
    <element name="country" type="xsd:string"/>
</sequence>

解決方案 #2

如果 WSDL 定義正確,則可更改輸入的 SOAP 酬載以符合 WSDL 定義。

例如,您可以修改輸入 SOAP 酬載,如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<shipOrder xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
    <shipTo>
        <name>Karl Kalckstein</name>
        <street>1020 Parkway</street>
        <address>Mountain View, CA 94043</address>
    </shipTo>
    <items>
        <item>
            <title>I love APIs</title>
            <quantity>1</quantity>
            <price>2.90</price>
        </item>
    </items>

原因:JSON 或 XML 格式錯誤

如果輸入的 SOAP 酬載不符合 SoapMessageValidation 政策 <ResourceURL> 元素中指定的 WSDL 定義,就會發生這個錯誤。

錯誤訊息範例

{
  "fault": {
    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Expected <\/title> at line 15(possibly  around char 420)\"",
    "detail": {
      "errorcode": "steps.messagevalidation.Failed"
    }
  }
}

診斷

  1. 找出 SOAPMessageValidation 政策名稱、失敗原因,以及與 WSDL 定義不符的 XML 酬載的行號。您可以在錯誤字串中找到所有這些資訊。舉例來說,在下列錯誤字串中,SOAPMessageValidation 政策名稱為 SOAP-Message-Validation-1,失敗原因為 Expected </title> at line 15(possibly around char 420):,行號為 15.

    "faultstring": "SOAP-Message-Validation-1 failed with reason: \"Expected </title> at line 15(possibly  around char 420)\""
    
  2. 檢查您在上方步驟 1 找到的行數的輸入酬載,瞭解失敗的原因。

    輸入 SOAP 酬載範例

    <?xml version="1.0"?>
    <soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
    <soap:Body>
    <shipOrder xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance">
      <shipTo>
        <name>Karl Kalckstein</name>
        <street>1020 Parkway</street>
        <address>Mountain View, CA 94043</address>
        <country>USA</country>
      </shipTo>
      <items>
        <item>
          <title>I love APIs</titles>
          <quantity>1</quantity>
          <price>2.90</price>
        </item>
      </items>
    </shipOrder>
    </soap:Body>
    </soap:Envelope>
    

    在上方範例中,輸入 XML 的第 15 行含有元素 <title>,但並未以 </title> 結束標記結束。

解析度

如要解決錯誤,請確認輸入酬載有效且格式正確。

例如,您可以修改輸入 XML 酬載,如下所示:

...
</shipTo>
<items>
  <item>
    <title>I love APIs</title>
    <quantity>1</quantity>
    <price>2.90</price>
  </item>
</items>
...