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 屬於訊息類型。如要進一步瞭解訊息變數,請參閱「變數參考資料」。

診斷

  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> 元素,指定「訊息」類型的變數。舉例來說,在失敗的 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 酬載未遵循 SoapMessageValidation 政策 <ResourceURL> 元素中指定的 XSD 結構定義,就會發生這個錯誤。

錯誤訊息範例

{
  "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 中識別的行號輸入酬載,瞭解導致失敗的原因。

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