如何在异常对象中找到Http错误有效载荷?
系统1->呼叫的Mule服务器。 Mule服务器-> Docusign /其他。
Docusign /有人使用JSON负载返回400错误(包括errorCode等)
我需要将确切的详细信息返回给系统1。
我将如何做?
1)我确实尝试将所有代码添加到成功代码200..599中,那么即使出错也有200 ok。这将引起更多的问题,因为随着一些流具有更多的数据转换,我必须添加很多选择路由。
2)理想溶液->像往常一样引发异常。这必须陷入选择异常中。现在,我必须将相同的status_code和json响应发送回调用方。
问题-?如何在异常对象中找到HTTP返回的有效负载。在哪里存储在异常对象中。
答案 0 :(得分:1)
这里是示例-处理这种情况的一种方法,使用validation Component
引发异常并通过exceptionHandling或APIKIT ExceptionHandling在您的主流中捕获
<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>
在您的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
。