【问题标题】:How to respond with status 500 (server error) when proxy fails?代理失败时如何响应状态 500(服务器错误)?
【发布时间】:2015-11-19 07:05:50
【问题描述】:

在我们的测试环境中,我们构建了一个单向代理服务,该服务使用 callout 从其他几个服务中获取数据。现在,当其中一项服务失败时,wso2 代理无论如何都会发送 202。是否可以“捕获异常”并返回不同的状态?

我知道在这种情况下使用请求-响应代理会更好,但即使我们创建一个仅将有效负载存储在消息存储中的代理,也可能存在关于存储有效负载的数据库的问题(数据库已关闭等),如果消息未存储在数据库中,我们不希望返回状态 202。

编辑:

在我的问题的第一个答案中建议更改后,这是我的代理:

<?xml version="1.0" encoding="UTF-8"?>
<proxy name="notifyOfClaimChangeOut" serviceGroup="" startOnLoad="true"
  trace="disable" transports="http https" xmlns="http://ws.apache.org/ns/synapse">
  <target>
    <inSequence>
      <property description="OUT_ONLY" name="OUT_ONLY" scope="default"
        type="BOOLEAN" value="true"/>
      <property name="FORCE_ERROR_ON_SOAP_FAULT" scope="default"
        type="STRING" value="true"/>
      <property description="OriginalPayload" expression="$body"
        name="OriginalPayload" scope="default" type="STRING"/>
      <iterate description=""
        expression="$body//InsClaimData/PartnerList/PartnerEntry" id="" sequential="true">
        <target>
          <sequence>
            <payloadFactory description="" media-type="xml">
              <format>
                <plat:FindCustomerSync xmlns:plat="http://platform.###.pl/">
                  <plat:request>
                    <plat:FirstName>$1</plat:FirstName>
                    <plat:LastName>$2</plat:LastName>
                    <plat:Pesel>$3</plat:Pesel>
                  </plat:request>
                </plat:FindCustomerSync>
              </format>
              <args>
                <arg evaluator="xml" expression="$body/PartnerEntry/BusinessPartner/personData/firstName"/>
                <arg evaluator="xml" expression="$body/PartnerEntry/BusinessPartner/personData/lastName"/>
                <arg evaluator="xml" expression="$body/PartnerEntry/BusinessPartner/personData/PESEL"/>
              </args>
            </payloadFactory>
            <log level="custom">
              <property expression="$body" name="property_name"/>
            </log>
            <callout action="http://platform.###.pl/FindCustomerSync"
              description="QueryCustomersOut"
              endpointKey="gov:###/endpoints/###/QueryCustomersOut.xml" initAxis2ClientOptions="false">
              <source type="envelope"/>
              <target key="QueryCustomersOutResponse"/>
            </callout>
            <log description="" level="custom" separator=",">
              <property expression="$ctx:QueryCustomersOutResponse"
                name="QueryCustomersOut"
                xmlns:cla="http://###.###.io/service/internal/ClaimService" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"/>
            </log>
          </sequence>
        </target>
      </iterate>
      <!-- 
      <callout description="ClaimService"
        endpointKey="gov:###/endpoints/###/ClaimService.xml" initAxis2ClientOptions="false">
        <source type="envelope"/>
        <target key="ClaimServiceResponse"/>
      </callout>
      <log description="ClaimService Response" level="custom">
        <property expression="$ctx:ClaimServiceResponse" name="ClaimServiceResponse"/>
      </log>
       -->
      <drop/>
    </inSequence>
    <outSequence/>
    <faultSequence>
      <property name="HTTP_SC" scope="axis2" type="STRING" value="500"/>
      <log>
        <property name="Error" value="Fault :("/>
      </log>
    </faultSequence>
  </target>
  <publishWSDL key="gov:###/schemas/###/ClaimService.wsdl"/>
</proxy>

【问题讨论】:

    标签: wso2 wso2esb esb


    【解决方案1】:

    您可以使用 HTTP_SC 属性发送状态 500。您还需要 makefault mediator 来构造故障消息。

    <property name="HTTP_SC" value="500" scope="axis2"/>
    <makefault version="soap11">
    

    您可以参考this jira 了解一下。

    另外,在inSequence 中,您可能需要设置FORCE_ERROR_ON_SOAP_FAULT 属性,以便在soap 故障发生时移动到故障序列。一些explanation available here

    <inSequence>
            <property name="FORCE_ERROR_ON_SOAP_FAULT" value="true" scope="default" type="STRING"/>
    </inSequence>
    

    【讨论】:

    • 好的,我确实在代理开始时设置了 FORCE_ERROR_ON_SOAP_FAULT 但是当我发送一个非法的 xml 时它仍然以 202 响应.. 所以.. 它不能解决我的问题。
    • 另外,如果我的端点(我的代理正在调用的服务的端点)不正确,代理会将错误记录到控制台并以 202.. 响应
    • 在您在我的第一个问题中提出的更改后,我添加了代理代码。
    • 另外,我认为您忽略了我正在创建单向代理这一事实
    • 我刚刚通过将它添加到 outSequence 来检查这个属性是否有效,它工作正常。请检查您的故障序列是否被执行。
    猜你喜欢
    • 1970-01-01
    • 2018-02-19
    • 2013-04-16
    • 1970-01-01
    • 2015-12-19
    • 2018-01-22
    • 2014-11-24
    • 2021-07-27
    相关资源
    最近更新 更多