我的 mule 流有一个如下所示的捕获异常策略:
<catch-exception-strategy when="org.mule.util.ExceptionUtils.containsType(exception, IllegalArgumentException.class)"
doc:name="Catch Exception Strategy">
<logger message="IllegalArgumentException occurred" level="INFO" doc:name="logInfo" />
</catch-exception-strategy>
表达式 org.mule.util.ExceptionUtils.containsType(exception, IllegalArgumentException.class)
将 null 作为第二个参数发送给方法 containsType
类 org.mule.util.ExceptionUtils
。当我将表达式更改为 org.mule.util.ExceptionUtils.containsType(exception, java.lang.IllegalArgumentException.class)
时,这一次 mule 说表达式错误并抛出异常。
所以我的问题是为什么表达式 org.mule.util.ExceptionUtils.containsType(exception, IllegalArgumentException.class)
将 null 发送到第二个参数?
谁能推荐一下?
最佳答案
如果您想对每种类型的异常实现不同的处理,您可以使用“选择异常策略”。这里有一个例子:
<flow name="Sample_Flow">
...
<choice-exception-strategy doc:name="Choice Exception Strategy">
<catch-exception-strategy doc:name="Catch Exception Strategy" when="#[exception.causedBy(org.mule.api.routing.filter.FilterUnacceptedException)]">
<set-variable variableName="errorStatusCode" value="404" doc:name="Set status code"/>
<set-variable variableName="errorReasonPhrase" value="Not Found" doc:name="Set reason phrase"/>
</catch-exception-strategy>
<rollback-exception-strategy doc:name="Rollback Exception Strategy">
<logger level="INFO" doc:name="Logger" message="Unknown error"/>
</rollback-exception-strategy>
</choice-exception-strategy>
</flow>
对于您的具体实现,您可以尝试只使用“IllegalArgumentException”而不是“llegalArgumentException.class”:
<catch-exception-strategy when="org.mule.util.ExceptionUtils.containsType(exception, IllegalArgumentException)">
<logger message="IllegalArgumentException occurred #[exception.getCauseException().getClass()] - #[IllegalArgumentException]: #[org.mule.util.ExceptionUtils.containsType(exception.getCauseException(), IllegalArgumentException)]" level="INFO" doc:name="logInfo" />
</catch-exception-strategy>
经过分析我发现表达式“IllegalArgumentException.class”返回的是null,但是表达式“IllegalArgumentException”返回的是类。
您可以在下一个链接中找到更多信息: https://docs.mulesoft.com/mule-user-guide/v/3.7/choice-exception-strategy
关于java - Mule MEL 用法和 ExceptionUtils.containsType(exception, type),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33566561/