排解 ServiceCallout 執行階段錯誤

您目前查看的是 ApigeeApigee Hybrid 說明文件。
查看 Apigee Edge 說明文件。

RequestVariableNotMessageType

錯誤代碼

steps.servicecallout.RequestVariableNotMessageType

錯誤回應主體

{
    "fault": {
        "faultstring": "ServiceCallout[POLICY_NAME]: request variable [VARIABLE_NAME] value is not of type Message",
        "detail": {
            "errorcode": "steps.servicecallout.RequestVariableNotMessageType"
        }
    }
}

原因

如果 ServiceCallout 政策<Request> 元素中指定的變數不是 message 類型,就會發生這個錯誤。如果變數是字串或任何其他非訊息類型,就會看到這項錯誤。

訊息型別變數代表整個 HTTP 要求和回應。內建流程變數 requestresponsemessage 的類型為 message

診斷

  1. 找出發生錯誤的 ServiceCallout 政策,以及類型有誤的變數名稱。您可以在錯誤回應的 faultstring 元素中找到這兩項資訊。舉例來說,在下列 faultstring 中,政策名稱為 ExecuteGeocodingRequest,變數為 PostalCode

    "faultstring": "ServiceCallout[ExecuteGeocodingRequest]: request variable PostalCode value is not of type Message"

  2. 在失敗的 ServiceCallout 政策 XML 中,確認 <Request> 元素中設定的變數名稱,是否與錯誤字串中識別的變數名稱相符 (如上方的步驟 1)。舉例來說,下列政策指定名為 PostalCode 的要求變數,與 faultstring 中的內容相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ServiceCallout name="ExecuteGeocodingRequest">
        <Request variable="PostalCode"/>
        <Response>GeocodingResponse</Response>
        <HTTPTargetConnection>
            <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
        </HTTPTargetConnection>
    </ServiceCallout>
    
  3. 判斷這個變數是否為訊息類型:

    1. 在 API Proxy 套件中找出程式碼,其中變數是先定義的。
    2. 在大多數情況下,您會發現問題變數是在 ServiceCallout 政策之前執行的另一個政策中建立及填入。舉例來說,Assign Message 政策通常用於在 API Proxy 流程中建立及填入變數。
    3. 找出變數定義和填入的政策後,請按照下列步驟判斷變數類型:
      • 檢查 type 屬性的值 (如有)。
      • 如果沒有 type 屬性,變數會視為字串。
    4. 如果變數的類型不是訊息 (例如字串),這就是造成錯誤的原因。如要瞭解常見變數及其類型,請參閱流程變數參考資料

舉例來說,假設 ServiceCallout 政策中參照的 PostalCode 變數是在下列 AssignMessage 政策中建立。請注意,PostalCode 會指派流程變數 request.queryparam.postalcode 的值。這個值是字串,因為變數指派中沒有 type 屬性。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="GenerateGeocodingRequest">
        <AssignTo createNew="true" type="request">GeocodingRequest</AssignTo>
    <Set>
        <QueryParams>
            <QueryParam name="address">{request.queryparam.postalcode}</QueryParam>
            <QueryParam name="region">{request.queryparam.country}</QueryParam>
            <QueryParam name="sensor">false</QueryParam>
        </QueryParams>
        <Verb>GET</Verb>
    </Set>
    <AssignVariable>
        <Name>PostalCode</Name>
        <Ref>request.queryparam.postalcode</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>Country</Name>
        <Ref>request.queryparam.country</Ref>
    </AssignVariable>
</AssignMessage>

現在,請回想一下 PostalCode 變數是用於 ServiceCallout 政策<Request> 元素:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout name="ExecuteGeocodingRequest">
    <Request variable="PostalCode"/>
    <Response>GeocodingResponse</Response>
    <HTTPTargetConnection>
        <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

由於 PostalCode 不是訊息類型 (在本例中為字串),因此您會收到錯誤代碼:steps.servicecallout.RequestVariableNotMessageType

解析度

請確認失敗的 ServiceCallout 政策中 <Request> 元素設定的變數是現有的 message 類型流程變數,或者您也可以直接在 ServiceCallout 政策中建立新的訊息類型變數 (如「ServiceCallout 政策」一文所述),並使用該變數。

如要修正政策,您必須修改 <Request> 元素,指定現有或新的訊息類型變數。舉例來說,在「指派訊息」政策中設定的 GeocodingRequest 變數屬於訊息類型,在 ServiceCallout 政策中也能正常運作。例如:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout name="ExecuteGeocodingRequest">
    <Request variable="GeocodingRequest"/>
    <Response>GeocodingResponse</Response>
    <HTTPTargetConnection>
        <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

RequestVariableNotRequestMessageType

錯誤代碼

steps.servicecallout.RequestVariableNotRequestMessageType

錯誤回應主體

{
    "fault": {
        "faultstring": "ServiceCallout[policy_name]: request variable [variable_name] value is not of type Request Message",
        "detail": {
            "errorcode": "steps.servicecallout.RequestVariableNotRequestMessageType"
        }
    }
}

原因

如果 ServiceCallout 政策<Request> 元素中指定的變數不是 message 類型,就會發生這個錯誤。如果變數是回應訊息類型、字串或其他類型,就會看到這項錯誤。

message 型別變數代表完整的 HTTP 要求和回應。內建流程變數 requestresponsemessage 的類型為 message

診斷

  1. 找出發生錯誤的 ServiceCallout 政策,以及類型有誤的變數名稱。您可以在錯誤回應的 faultstring 元素中找到這兩項資訊。舉例來說,在下列 faultstring 中,政策名稱為 ExecuteGeocodingRequest,變數為 var_response

    "faultstring": "ServiceCallout[ExecuteGeocodingRequest]: request variable var_response value is not of type Message"

  2. 在失敗的 ServiceCallout 政策 XML 中,確認 <Request> 元素中設定的變數名稱,是否與錯誤字串中識別的變數名稱相符 (如上方的步驟 1)。舉例來說,下列政策指定名為 var_response 的要求變數,與 faultstring 中的內容相符:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ServiceCallout name="ExecuteGeocodingRequest">
        <Request variable="var_response"/>
        <Response>GeocodingResponse</Response>
        <HTTPTargetConnection>
            <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
        </HTTPTargetConnection>
    </ServiceCallout>
    
  3. 判斷變數是否為要求訊息類型:

    1. 在 API Proxy 套件中找出程式碼,其中變數是先定義的。
    2. 在大多數情況下,您會發現問題變數是在 ServiceCallout 政策之前執行的另一個政策中建立及填入。舉例來說,Assign Message 政策通常用於在 API Proxy 流程中建立及填入變數。
    3. 找出變數定義和填入的政策後,請按照下列步驟判斷變數類型:
      • 檢查 type 屬性的值 (如有)。
      • 如果沒有 type 屬性,變數會視為字串。
    4. 如果變數的類型不是要求訊息類型,就是導致錯誤的原因。如要瞭解常見變數及其類型,請參閱流程變數參考資料

舉例來說,假設在 ServiceCallout 政策中參照的 var_response 變數,是在下列指派訊息政策中建立。請注意,var_response 的類型為 response。因此,var_response 變數的型別是回應訊息。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="GenerateGeocodingRequest">
        <AssignTo createNew="true" type="request">GeocodingRequest</AssignTo>
    <AssignTo createNew="true" type="response">var_response</AssignTo>
    <Set>
        <QueryParams>
            <QueryParam name="address">{request.queryparam.postalcode}</QueryParam>
            <QueryParam name="region">{request.queryparam.country}</QueryParam>
            <QueryParam name="sensor">false</QueryParam>
        </QueryParams>
        <Verb>GET</Verb>
    </Set>
    <AssignVariable>
        <Name>PostalCode</Name>
        <Ref>request.queryparam.postalcode</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>Country</Name>
        <Ref>request.queryparam.country</Ref>
    </AssignVariable>
</AssignMessage>

回想一下,var_response 變數是用於 ServiceCallout 政策的 <Request> 元素。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout name="ExecuteGeocodingRequest">
    <Request variable="var_response"/>
    <Response>GeocodingResponse</Response>
    <HTTPTargetConnection>
        <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

由於 var_response 不是要求訊息類型 (其類型為回應訊息),因此您會收到錯誤代碼:steps.servicecallout.RequestVariableNotRequestMessageType

解析度

請確認失敗的 ServiceCallout 政策中 <Request> 元素設定的變數是現有的 message 型別變數,或者您也可以直接在 ServiceCallout 政策中建立新的要求訊息型別變數 (如「ServiceCallout 政策」一文所述),並使用該變數。

如要修正政策,請修改 <Request> 元素,指定現有或新的要求訊息類型變數,這樣 ServiceCallout 政策就能正常運作。例如:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout name="ExecuteGeocodingRequest">
    <Request variable="GeocodingRequest"/>
    <Response>GeocodingResponse</Response>
    <HTTPTargetConnection>
        <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

ExecutionFailed

錯誤代碼

steps.servicecallout.ExecutionFailed

錯誤回應主體

{
    "fault": {
        "faultstring": "Execution of ServiceCallout [policy_name] failed. Reason: Host not reachable",
        "detail": {
            "errorcode": "steps.servicecallout.ExecutionFailed"
        }
    }
}

{
    "fault": {
        "faultstring": "Execution of ServiceCallout [policy_name] failed. Reason: ResponseCode [http_code] is treated as error",
        "detail": {
            "errorcode": "steps.servicecallout.ExecutionFailed"
        }
    }
}

可能原因

這項錯誤的可能原因包括:

原因 說明
網址無效或格式有誤 ServiceCallout 政策中的目標網址格式有誤,或主機名稱無效/無法連線。
後端伺服器錯誤 後端伺服器傳回 4XX 或 5XX 的錯誤回應。

原因:網址無效或格式有誤

ServiceCallout 政策中的目標網址格式有誤,或主機名稱無效/無法連線。

診斷

  1. 找出導致錯誤的 ServiceCallout 政策。政策名稱會顯示在錯誤回應的 faultstring 元素中。舉例來說,在下列 faultstring 中,失敗的 ServiceCallout 政策名稱為 ExecuteGeocodingRequest

    "faultstring": "ServiceCallout[ExecuteGeocodingRequest]"

  2. 在失敗的 ServiceCallout 政策中,檢查 <URL> 元素。如果格式錯誤或主機名稱無效/無法連線,就會導致這個錯誤。舉例來說,下列 ServiceCallout 政策指定了無效的 <URL>

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ServiceCallout name="ExecuteGeocodingRequest">
        <Request variable="GeocodingRequest"/>
        <Response>GeocodingResponse</Response>
        <HTTPTargetConnection>
            <URL>http://</URL>
        </HTTPTargetConnection>
    </ServiceCallout>
    

    <URL> 元素只有通訊協定 http://,但沒有有效的主機名稱,因此 ServiceCallout 政策會失敗,並顯示錯誤:Execution of ServiceCallout ExecuteGeocodingRequest failed. Reason: Host not reachable

解析度

確認失敗的 ServiceCallout 政策中的 <URL> 元素具有可連線主機名稱的有效網址。

如要修正上述 ServiceCallout 政策,可以修改 <URL> 元素,指定有效網址:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ServiceCallout name="ExecuteGeocodingRequest">
    <Request variable="GeocodingRequest"/>
    <Response>GeocodingResponse</Response>
    <HTTPTargetConnection>
        <URL>http://maps.googleapis.com/maps/api/geocode/json</URL>
    </HTTPTargetConnection>
</ServiceCallout>

原因:後端伺服器發生錯誤

後端伺服器傳回 4XX 或 5XX 錯誤回應。

診斷

  1. 找出導致錯誤的 ServiceCallout 政策。政策名稱會顯示在錯誤回應的 faultstring 元素中。舉例來說,在下列 faultstring 中,失敗的 ServiceCallout 政策名稱為 ExecuteGeocodingRequest

    "faultstring": "ServiceCallout[ExecuteGeocodingRequest]

  2. 檢查錯誤回應主體中的 faultstring,並確認 Reason 中是否列出任何 4XX 或 5XX 回應碼。舉例來說,下列 faultstring 清楚指出後端伺服器傳回了 502 回應代碼:

    "faultstring": "Execution of ServiceCallout ExecuteGeocodingRequest failed. Reason: ResponseCode 502 is treated as error"

解析度

判斷錯誤回應代碼後,即可像處理任何 4XX 或 5XX 錯誤一樣,排解這個問題。