问题
I have a Mule flow that evaluates a payload an depending on the value, may throw an exception using Groovy. My flow looks as follows:
<flow name="test-flow" doc:name="test-flow">
<vm:inbound-endpoint path="testexception.in" exchange-pattern="request-response" doc:name="VM"/>
<choice doc:name="Choice">
<when expression="#[payload == 'Goodbye']">
<logger message="**************** #[payload] ****************" level="INFO" doc:name="Logger"/>
</when>
<otherwise>
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[throw new Exception('We have an error')]]></scripting:script>
</scripting:component>
</otherwise>
</choice>
<catch-exception-strategy doc:name="Catch Exception Strategy">
<logger level="INFO" message="*** The exception is: #[exception] ***" doc:name="Logger"/>
<logger level="INFO" message="There has been an error" doc:name="Logger"/>
</catch-exception-strategy>
</flow>
The error I see when I send a message like 'Hello' for example is something like the below:
ERROR 2014-05-29 12:18:39,707 [Thread-0] org.mule.exception.CatchMessagingExceptionStrategy:
********************************************************************************
Message : Failed to invoke ScriptComponent{test-flow.component.1695047076}. Component that caused exception is: ScriptComponent{test-flow.component.1695047076}. Message payload is of type: String
Code : MULE_ERROR--2
--------------------------------------------------------------------------------
Exception stack is:
1. We have an error (java.lang.Exception)
sun.reflect.NativeConstructorAccessorImpl:-2 (null)
2. java.lang.Exception: We have an error (javax.script.ScriptException)
org.codehaus.groovy.jsr223.GroovyScriptEngineImpl:323 (http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/script/ScriptException.html)
3. Failed to invoke ScriptComponent{test-flow.component.1695047076}. Component that caused exception is: ScriptComponent{test-flow.component.1695047076}. Message payload is of type: String (org.mule.component.ComponentException)
org.mule.component.AbstractComponent:148 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/component/ComponentException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.Exception: We have an error
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************
Is there a way I can access the first item of the exception stack so that I can evaluate the value and ensure my error has been thrown correctly? In this case I'm looking to access the value 'We have an error'. I thought the solution was in using #[exception]
but this gives me something like org.mule.component.ComponentException: Failed to invoke ScriptComponent{test-flow.component.1695047076}. Component that...
which isn't what I'm after. Any help or guidance is appreciated, thanks in advance.
回答1:
The org.mule.component.ComponentException
should wrap your custom exception, maybe several level deep.
To help, Mule provides ExceptionUtils to help digging in the exception and finding the one you want.
来源:https://stackoverflow.com/questions/23932785/mule-accessing-the-exception-stack-and-logging-a-particular-part