Mule: getting a hold of the exception message
Asked Answered
A

5

6

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,

Achilles answered 28/4, 2014 at 14:10 Comment(3)
Can you share the error message from logsSituation
After some time I figured out that my issue was due to nesting #[] where they shouldn't have been nested. However I still have an issue, #[exception] will return the Mule top error, which isn't very interesting in what really happened. I'm just getting invoking a component failed, while the real error I want to output is the root error (permission denied on a directory creation from sftp.) Any way to get the root error message from the #[exception] object?Achilles
#[exception.getCause()] actually gets me what I wantAchilles
S
4

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

Situation answered 28/4, 2014 at 14:57 Comment(0)
L
9

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.

Leger answered 6/7, 2015 at 0:1 Comment(0)
C
9

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

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

Combust answered 31/8, 2016 at 5:58 Comment(2)
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!Farrell
@Rashiki the question mark is a check for a null pointer exception.Xanthic
S
4

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

Situation answered 28/4, 2014 at 14:57 Comment(0)
K
3

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]
Kutaisi answered 9/10, 2014 at 6:40 Comment(0)
A
1

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

#[exception.?cause.message or exception]
Ambrosial answered 13/6, 2017 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.