Mulesoft - 如何在异常对象中查找 Http 错误负载?

Mulesoft - How to find Http error payload in exception object?

如何在异常对象中查找 Http 错误负载?

系统 1 -> Call 的 Mule 服务器。 Mule 服务器 -> Docusign / 其他服务器。

Docusign / 某个 returns 400 错误 json 有效载荷(包括错误代码等)

我需要return返回系统 1 的确切细节。

我该怎么做?

1) 我确实尝试在成功代码 200..599 中添加所有代码,然后甚至错误是 200 ok 。这会导致更多问题,因为我必须添加很多选择路由,因为一些流有更多的数据转换。

2) 理想的解决方案 -> 像往常一样引发异常。这必须在选择异常中被捕获。现在我必须将相同的 status_code 和 json 响应发送回调用方。

问题-?我如何在异常对象中找到由 HTTP 编辑的负载 return。存储在异常对象中的位置。

这是示例 - 处理这种情况的一种方法,使用 validation Component 抛出异常并通过异常处理或 APIKIT 异常处理在您的主流程中捕获

 <flow name="routeexceptionFlow">
    <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
    <logger level="INFO" doc:name="Logger"/>
    <http:request config-ref="HTTP_Request_Configuration" path="/in" method="POST" doc:name="HTTP">
        <http:success-status-code-validator values="200..599"/>
    </http:request>
    <validation:is-false config-ref="Validation_Configuration" message="#[payload]" exceptionClass="nz.co.exception.BadRequestException" expression="#[message.inboundProperties['http.status'] ==400]" doc:name="Validate Bad Request"/>
    <validation:is-false config-ref="Validation_Configuration" message="#[payload]" expression="#[message.inboundProperties['http.status'] !=200]" doc:name="Capture all other Exceptions"/>
    <choice-exception-strategy doc:name="Choice Exception Strategy">
        <catch-exception-strategy when="#[exception.causeMatches('nz.co.exception.BadRequestException')]" doc:name="Catch Exception Strategy">
            <set-payload value="#[exception.message]" doc:name="Set Payload"/>
            <logger level="INFO" doc:name="Logger"/>
        </catch-exception-strategy>
        <catch-exception-strategy doc:name="Catch Exception Strategy">
            <logger message="****log all other exception****" level="INFO" doc:name="Logger"/>
        </catch-exception-strategy>
    </choice-exception-strategy>
</flow>

将此 class 添加到您的 java 文件夹中

  package nz.co.exception;

   public class BadRequestException extends Exception {
   private static final long serialVersionUID = 8546839078847602529L;

    public BadRequestException() {
    super();
    }

    public BadRequestException(String message) {
    super(message);
    }

   public BadRequestException(Throwable t) {
    super(t);
   }

  public BadRequestException(String message, Throwable t) {
    super(message, t);
  }
}

在你的项目中,如果你使用的是 APIKIT Router ,那么不要像上面那样使用 exceptionHandling,而是直接在 400 ReturnCode apikitExceptionHandling 中添加 nz.co.exception.BadRequestException