✅ Prevent weird stacktrace text in the log when throw it from java?

13
In a microflow I use the UserThrowException java action from the Community Commons and I catch this error in a custom java action and is logged in this way: logger.error("Error during execution of microflow " + microflowName + ","+ e.getMessage()); I expect a message in the log like this: Dec 3 13:07:10.036 - ERROR - Module: Error during execution of microflow xyz. Failed blabla functional exception message Instead we receive 3 extra lines in the log like this: Dec 3 13:07:10.036 - ERROR - Module: (1/4) Error during execution of microflow xyz. com.mendix.modules.microflowengine.MicroflowException: com.mendix.core.CoreRuntimeException: com.mendix.systemwideinterfaces.MendixRuntimeException: communitycommons.UserThrownException: blabla functional message Dec 3 13:07:10.036 - ERROR - Module: (2/4) #011at Module.xyz (JavaAction : 'ThrowException') Dec 3 13:07:10.036 - ERROR - Module: (3/4) Dec 3 13:07:10.036 - ERROR - Module: (4/4) Advanced stacktrace: (no stacktrace and that is good). My question is how do I 🤬 get rid of the text about com.mendix.coreblalabla etc before en at the end of my logmessage?  When I log an Error Log message from a microflow (without the boolean stacktrace) it's all perfect like this: Dec 3 13:07:10.036 - ERROR - Module: Error during execution of microflow xyz. Failed blabla functional exception message   Any java expert in the house? thank you! 🙌🏻   EDIT: This is the solution in the java code: Throwable cause = e; while( cause.getCause() != null ) { cause = cause.getCause(); } logger.error("Error during execution of microflow " + microflowName + "." + cause.getMessage()); Thanks Andrej! Appreciated 😁
asked
2 answers
6

The problem is that the exception goes through several layers of abstraction and at every level it is wrapped in another exception with a different class. You need to navigate the wrapped exception list to find your exception. I picked up the following code from Stackoverflow

    Exception cause = e;
    MyException my_exception= null;
    while( cause != null && !( cause instanceof MyException) ) {
        my_exception = (MyException) cause;
        cause = cause.getCause();
    } 
    if ( my_exception != null )
           // DO SOMETHING
    else 
           // it must be another exception
answered
2

Hello Pim, 

I've previously looked into obtaining a clear message and couldn't find a proper solution - to me it looks like the ThrowException java action is simply wrapping the default Exception class and raising it as normal, and Mendix seems to be adding the extra trace to it (I might be wrong).

We've worked around it by adding a tag to the exception and then catching all errors. If the tag is found in the caught error we exclude the stacktrace from the error message since it's of no use, we also use this to display custom messages to users.

It's quite dirty but we couldn't find any better way. 

Disclaimer: It seems images might not fit within the display window - if they seem cut off you might need to download them for the whole picture.

answered