Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception Mule在HTTP响应中设置状态代码_Exception_Mule - Fatal编程技术网

Exception Mule在HTTP响应中设置状态代码

Exception Mule在HTTP响应中设置状态代码,exception,mule,Exception,Mule,我需要使用mule Choice异常策略在mule HTTP中设置状态代码和原因短语,该策略是Catch异常策略。遵循Mulesoft文档 我试过这样做: <choice-exception-strategy name="MyExptn"> <rollback-exception-strategy when="#[exception.causedBy(org.mule.component.ComponentException)]" doc:name="ComponentEx

我需要使用mule Choice异常策略在mule HTTP中设置状态代码和原因短语,该策略是Catch异常策略。遵循Mulesoft文档

我试过这样做:

  <choice-exception-strategy name="MyExptn">
<rollback-exception-strategy when="#[exception.causedBy(org.mule.component.ComponentException)]" doc:name="ComponentException Strategy">

        <set-variable variableName="errorTableNotPresent" value="400" doc:name="Set status code"/>
        <set-variable variableName="errorReasonPhrase" value="Table name does not exist" doc:name="Set reason phrase"/>
        <message-properties-transformer doc:name="Message Properties">
        <add-message-property key="timestamp" value="#[server.dateTime.format('yyyy-MM-dd hh:mm:ss.SS')]"/>
        <add-message-property key="messageID" value="#[message.id]"/>
        <add-message-property key="status" value="Error"/>
        <add-message-property key="executionPoint" value="Error Handling"/>
        <add-message-property key="ip" value="#[server.ip]"/>
        <add-message-property key="serverName" value="#[server.host]"/>
        <add-message-property key="domainname" value="xyz.com"/>
        <add-message-property key="errorCode" value="123"/>
        <add-message-property key="errorType" value="Bad request"/>
        <add-message-property key="errorText" value="ComponentException"/>
        <add-message-property key="integrationName" value="${integrationName}"/>
        <add-message-property key="resourceType" value="HTTP Request"/>
        <add-message-property key="resourceName" value="Payload"/>
    </message-properties-transformer>
        <logger level="INFO" message="#[LoggingAppendString] - Failure" doc:name="Logger"/>

    </rollback-exception-strategy>

</choice-exception-strategy>

这很好用。但当我用如下所示的捕获异常策略替换它时:

<choice-exception-strategy name="MyExptn">
<catch-exception-strategy when="#[exception.causedBy(org.mule.component.ComponentException)]" doc:name="ComponentException Strategy">

    <set-variable variableName="errorTableNotPresent" value="400" doc:name="Set status code"/>
    <set-variable variableName="errorReasonPhrase" value="Table name does not exist" doc:name="Set reason phrase"/>
    <message-properties-transformer doc:name="Message Properties">
    <add-message-property key="timestamp" value="#[server.dateTime.format('yyyy-MM-dd hh:mm:ss.SS')]"/>
    <add-message-property key="messageID" value="#[message.id]"/>
    <add-message-property key="status" value="Error"/>
    <add-message-property key="executionPoint" value="Error Handling"/>
    <add-message-property key="ip" value="#[server.ip]"/>
    <add-message-property key="serverName" value="#[server.host]"/>
    <add-message-property key="domainname" value="xyz.com"/>
    <add-message-property key="errorCode" value="123"/>
    <add-message-property key="errorType" value="Bad request"/>
    <add-message-property key="errorText" value="ComponentException"/>
    <add-message-property key="integrationName" value="${integrationName}"/>
    <add-message-property key="resourceType" value="HTTP Request"/>
    <add-message-property key="resourceName" value="Payload"/>
</message-properties-transformer>
    <logger level="INFO" message="#[LoggingAppendString] - Failure" doc:name="Logger"/>

</catch-exception-strategy>

</choice-exception-strategy>

将只记录消息,但HTTP状态始终显示200 OK


我的基本要求是根据错误发送状态代码。例如,如果请求不正确,则为错误请求,代码为400

定义两个出站属性:http.status和http.reason。这些将作为实际状态发送到客户端。如果不设置这些值,Mule将默认为200-OK

我看不到您的示例中定义了这些字段

您提供的文档链接中的示例:

<set-property propertyName="http.status" value="500" doc:name="Property"/>
<set-property propertyName="http.reason" value="Request successfully executed!" doc:name="Property"/>

请确保将这些范围作为出站属性

请记住,在运行时的某些版本上,存在一个错误,http.reason未正确映射到响应中,导致类似于状态400-OK而不是状态400-Bad请求的错误。

显示您的配置以及您遇到的错误?是。它仅适用于回滚异常策略。不知道为什么,但它不适用于捕获异常策略。