jvas
jvas

Reputation: 440

Get Exception message from mule

I am upgrading from Mule 3.4 to 3.7. I have applications that handle exceptions by alerting administrators by email. In 3.4, I used to have the mule expression #[exception.getSummaryMessage()] for printing and emailing the information. It used to provide me the complete exception message with the exception stack as below:


Message : groovy.lang.MissingPropertyException: No such property: z for class: Script1 (javax.script.ScriptException) Type : org.mule.api.transformer.TransformerException Code : MULE_ERROR--2 JavaDoc : http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html Transformer : ScriptTransformer{this=11810917, name='ScriptTransformer', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='/', encoding='null'}, sourceTypes=[]}


Exception stack is: 1. No such property: z for class: Script1 (groovy.lang.MissingPropertyException) org.codehaus.groovy.runtime.ScriptBytecodeAdapter:53 (null) 2. groovy.lang.MissingPropertyException: No such property: z for class: Script1 (javax.script.ScriptException) org.codehaus.groovy.jsr223.GroovyScriptEngineImpl:326 (http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/script/ScriptException.html) 3. groovy.lang.MissingPropertyException: No such property: z for class: Script1 (javax.script.ScriptException) (org.mule.api.transformer.TransformerException) org.mule.module.scripting.transformer.ScriptTransformer:56 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)


In 3.7, the same expression only gives me the exception message as below: groovy.lang.MissingPropertyException: No such property: z for class: Script1 (javax.script.ScriptException). Message payload is of type: NullPayload (org.mule.api.transformer.TransformerMessagingException). Message payload is of type: NullPayload

Could someone guide me as to how I can retrieve the exception in the previous format or how this can be achieved with Mule 3.7?

Upvotes: 0

Views: 1171

Answers (2)

2ark0
2ark0

Reputation: 107

I had to do something similar so I wrote a piece of Groovy code to extract the error message from the exception object:

def log = LogFactory.getLog(logCategory)
def cleanRootErrorMessage = null

// no matter what, we don't want Exceptions getting out of here!
try {
        log.debug("Extracting exception root cause...")
        def tmpException = exception

        while (tmpException.cause != null && (tmpException.cause instanceof NullPointerException) == false) {
                tmpException = tmpException.cause
        }

        cleanRootErrorMessage = tmpException.message
        log.debug("Root cause: [$cleanRootErrorMessage]")

        if (cleanRootErrorMessage ==~/^.*(Exception: |Error: )(.*)$/) {
                cleanRootErrorMessage = (cleanRootErrorMessage =~/^.*(Exception: |Error: )(.*)$/).replaceAll('$2')
        }

        if(cleanRootErrorMessage == null) {
                log.debug("Capturing exception.message...")
                cleanRootErrorMessage = exception.message
        }

        log.debug("Filtered root cause: [$cleanRootErrorMessage]")
} catch (Exception e) {
        log.error("Exception thrown ***inside*** the Root Error Message extraction code...", e)
        cleanRootErrorMessage = 'Unknown Error'
}

return cleanRootErrorMessage

I hope it helps, if you want further help, feel free to find me on my twitter account from my profile.

Upvotes: 1

Satheesh Kumar
Satheesh Kumar

Reputation: 797

Can you try #[exception.cause.message]

Upvotes: 0

Related Questions