“Executed multiple catch exception strategy ”

陌路散爱 提交于 2019-12-13 03:21:06

问题


I have choice exception strategy where I have multiple catch exception strategy there I am applying condition based on the error.

Error 1:

org.mule.api.MessagingException: Column 'department_id' cannot be null (java.sql.SQLIntegrityConstraintViolationException).

Error 2:

org.mule.api.MessagingException: org.mule.module.db.internal.domain.connection.ConnectionCreationException: Cannot get connection for URL jdbc:mysql://localhost:3306/mulesoft : Access denied for user 'root1212'@'localhost' (using password: YES) (java.sql.SQLException) (org.mule.module.db.internal.processor.DbConnectionException).

How can I differentiate between both of the error using expression in catch exception strategy?

First catch- Executed when:

[exception.causeMatches("org.mule.api.MessagingException: Column 'department_id' cannot be null*")]

Second catch- Executed when:

[exception.causeMatches("org.mule.api.MessagingException: org.mule.module.db.internal.domain.connection.ConnectionCreationException*")]

By using this not able to trigger into catch exception strategy.

[exception.causeMatches("org.mule.api.MessagingException*")]

This is working but for both the error getting same starting string. How can i differentiate both?


回答1:


If you want to catch specific exceptions based on the root cause. Use causedBy method on exception with the fully qualified exception name. Below examples handles 2 specific exceptions and then a default catch for all other exceptions.

 <choice-exception-strategy>
      <catch-exception-strategy when="#[exception.causedBy(com.ricston.CustomException)">
        <!-- do stuff -->
      </catch-exception-strategy>
      <catch-exception-strategy when="#[exception.causedBy(org.mule.module.db.internal.domain.connection.ConnectionCreationException)">
        <!-- do stuff -->
      </catch-exception-strategy>
      <catch-exception-strategy>
        <!-- other exceptions. do stuff -->
      </catch-exception-strategy>
    </choice-exception-strategy>


来源:https://stackoverflow.com/questions/56034753/executed-multiple-catch-exception-strategy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!