问题
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