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,

Loic Duros
  • 5,472
  • 10
  • 43
  • 56
  • Can you share the error message from logs – Charu Khurana Apr 28 '14 at 14:23
  • 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? – Loic Duros Apr 28 '14 at 14:39
  • 1
    #[exception.getCause()] actually gets me what I want – Loic Duros Apr 28 '14 at 14:54

5 Answers5

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.

FormigaNinja
  • 1,571
  • 1
  • 24
  • 36
Prathiba
  • 91
  • 1
  • 2
9

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

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

Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
  • 1
    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. https://docs.mulesoft.com/mule-user-guide/v/3.8/mule-expression-language-tips Thanks! – Hoonerbean Mar 15 '17 at 20:04
  • @Rashiki the question mark is a check for a null pointer exception. – granthbr Mar 31 '17 at 13:36
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

Charu Khurana
  • 4,511
  • 8
  • 47
  • 81
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]
Nicolas
  • 482
  • 4
  • 16
1

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

#[exception.?cause.message or exception]
pulsar
  • 46
  • 3