Mule: getting a hold of the exception message

20,534

Solution 1

You can do #[exception.causedBy] like

   <choice-exception-strategy>
        <catch-exception-strategy when="exception.causedBy(com.company.BusinessException)"> <!-- [1] -->
            <jms:outbound-endpoint queue="dead.letter">
                <jms:transaction action="ALWAYS_JOIN" />
            </jms:outbound-endpoint>
        </catch-exception-strategy>
        <rollback-exception-strategy when="exception.causedBy(com.company.NonBusinessException)"> <!-- [2] -->
            <logger level="ERROR" message="Payload failing: #[payload]"/>
        </rollback-exception-strategy>
    </choice-exception-strategy>

More details here

Solution 2

In some cases the exception.cause could be null, hence advised to use the conditional to display the message:

[(exception.cause!=null)?(exception.cause.message):exception]

This will prevent null pointer exception.

Solution 3

Best way to get exception message (null safe) is :

#[exception.cause.?message or exception.cause]

Solution 4

Hi if you want to get the exception message using MEL you can also (in a Catch Exception Strategy) use the following expression.

#[exception.cause.message]

Solution 5

exception.cause could be null so it could be handled like this:

#[exception.?cause.message or exception]
Share:
20,534
Loic Duros
Author by

Loic Duros

Updated on July 19, 2022

Comments

  • Loic Duros
    Loic Duros almost 2 years

    I have a default catch exception in Mule, and I'm trying to get access to the exception message, using a Mule expression: #[exception]

    This doesn't seem to work, and I'm guessing that I'm trying to access the wrong variable? I'm trying to log it using logger and also run a custom component that takes in an exception message (as a string.)

    Thanks,

  • Hoonerbean
    Hoonerbean about 7 years
    Hi @Nirmal- thInk beYond. What is the question mark for in your answer? What does it do? Thanks! ---Edit--- Never mind! I just found the answer here. docs.mulesoft.com/mule-user-guide/v/3.8/… Thanks!
  • granthbr
    granthbr about 7 years
    @Rashiki the question mark is a check for a null pointer exception.