Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Grab Bits and Bytes From a Mule Exception Class

DZone's Guide to

How to Grab Bits and Bytes From a Mule Exception Class

Most of us usually use #[exception] to log errors in our mule flows. Let's see how we can get most out of the exception class in our mule flows.

· Integration Zone ·
Free Resource

Continue to drive demand for API management solutions that address the entire API life cycle and bridge the gap to microservices adoption.  

Most of us usually use #[exception] to log errors in our mule flows. Let's see how can we get most out of the exception class in our mule flows.

Let's take a scenario where we get a connection timeout exception over an http rest call.

Image title

Take a closer look at what gets logged using the below MELs when used in the Logger component present a Catch Exception Strategy.

1. Exception- #[exception] 

Exception- org.mule.api.MessagingException: Error sending HTTP request. 

2. Exception Class- #[exception.getClass()]

Exception Class- class org.mule.api.MessagingException

3. Exception Cause- #[exception.getCause()]

Exception Cause- java.net.ConnectException: Connect timeout

4. Exception Info- #[exception.getInfo()]    

Exception Info- {Payload Type=java.lang.String, Element=/sampleFlow1/processors/1 @   sample:sample.xml:16 (HTTP), Payload=, Element XML=<http:request config-           ref="HTTP_Request_Configuration" path="/Ok" method="GET" doc:name="HTTP">     </http:request>}

5. Exception Message- #[exception.getMessage()]    

Exception Message- Error sending HTTP request.

6. Exception Detailed Message- #[exception.getDetailedMessage()]

Exception Detailed Message- 
********************************************************************************
Message : Error sending HTTP request.
Payload : 
Payload Type : java.lang.String
Element : /sampleFlow1/processors/1 @ sample:sample.xml:16 (HTTP)
Element XML : <http:request config-ref="HTTP_Request_Configuration" path="/Ok" method="GET" doc:name="HTTP"></http:request>
--------------------------------------------------------------------------------
Root Exception stack trace:
java.net.ConnectException: Connect timeout
at org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectCompletionHandler.cancelled(SingleEndpointPool.java:1284)
at org.glassfish.grizzly.impl.SafeFutureImpl.notifyCompletionHandlers(SafeFutureImpl.java:185)
at org.glassfish.grizzly.impl.SafeFutureImpl.done(SafeFutureImpl.java:277)
at org.glassfish.grizzly.impl.SafeFutureImpl$Sync.innerCancel(SafeFutureImpl.java:389)
at org.glassfish.grizzly.impl.SafeFutureImpl.cancel(SafeFutureImpl.java:247)
at org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectTimeoutWorker.doWork(SingleEndpointPool.java:1352)
at org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectTimeoutWorker.doWork(SingleEndpointPool.java:1344)
at org.glassfish.grizzly.utils.DelayedExecutor$DelayedRunnable.run(DelayedExecutor.java:158)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)

********************************************************************************

7. Exception Summary Message- #[exception.getSummaryMessage()]

Exception Summary Message- 
********************************************************************************
Message : Error sending HTTP request.
Element : /sampleFlow1/processors/1 @ sample:sample.xml:16 (HTTP)
--------------------------------------------------------------------------------
Exception stack is:
Error sending HTTP request. (org.mule.api.MessagingException)
org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectCompletionHandler.cancelled(SingleEndpointPool.java:1284)
org.glassfish.grizzly.impl.SafeFutureImpl.notifyCompletionHandlers(SafeFutureImpl.java:185)
org.glassfish.grizzly.impl.SafeFutureImpl.done(SafeFutureImpl.java:277)
org.glassfish.grizzly.impl.SafeFutureImpl$Sync.innerCancel(SafeFutureImpl.java:389)
org.glassfish.grizzly.impl.SafeFutureImpl.cancel(SafeFutureImpl.java:247)
org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectTimeoutWorker.doWork(SingleEndpointPool.java:1352)
org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectTimeoutWorker.doWork(SingleEndpointPool.java:1344)
org.glassfish.grizzly.utils.DelayedExecutor$DelayedRunnable.run(DelayedExecutor.java:158)
org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
java.lang.Thread.run(Thread.java:745)

(set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

8. Exception Verbose Message- #[exception.getVerboseMessage()]

Exception Verbose Message- 
********************************************************************************
Message : Error sending HTTP request.
Payload : 
Payload Type : java.lang.String
Element : /sampleFlow1/processors/1 @ sample:sample.xml:16 (HTTP)
Element XML : <http:request config-ref="HTTP_Request_Configuration" path="/Ok" method="GET" doc:name="HTTP"></http:request>
--------------------------------------------------------------------------------
Root Exception stack trace:
java.net.ConnectException: Connect timeout
at org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectCompletionHandler.cancelled(SingleEndpointPool.java:1284)
at org.glassfish.grizzly.impl.SafeFutureImpl.notifyCompletionHandlers(SafeFutureImpl.java:185)
at org.glassfish.grizzly.impl.SafeFutureImpl.done(SafeFutureImpl.java:277)
at org.glassfish.grizzly.impl.SafeFutureImpl$Sync.innerCancel(SafeFutureImpl.java:389)
at org.glassfish.grizzly.impl.SafeFutureImpl.cancel(SafeFutureImpl.java:247)
at org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectTimeoutWorker.doWork(SingleEndpointPool.java:1352)
at org.glassfish.grizzly.connectionpool.SingleEndpointPool$ConnectTimeoutWorker.doWork(SingleEndpointPool.java:1344)
at org.glassfish.grizzly.utils.DelayedExecutor$DelayedRunnable.run(DelayedExecutor.java:158)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
at java.lang.Thread.run(Thread.java:745)

********************************************************************************

9. Exception Code- #[exception.getExceptionCode()]

Exception Code- -1

10. Exception Message Code- #[exception.getMessageCode()]

Exception Message Code- -1


11. Get Flow name that generated the Exception- 

#[exception.event.flowConstruct.name]    



You can actually try all of these in your MELs and get the most out of a mule exception class, so grab the best-suited one for your project. 

Thanks for reading! I hope this helps. Suggestions are welcomed in the comments.

Discover how organizations are modernizing their application architectures for speed and agility from the growing API economy

Topics:
mule ,mule esb ,anypoint studio ,mulesoft

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}