Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Avoid Printing Exception Stack Traces

DZone's Guide to

How to Avoid Printing Exception Stack Traces

Stack traces have been around in the software development world for quite some time now, but it can't be ignored that they can create major issues for Operations teams.

Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

This article explains how to avoid printing exception stack traces when running APIs that are configured with Mule EE runtime and Mule API gateway runtime.

Printing exception stack traces is not a new concept in the software world. However, it's still important to remember that if the stack traces are not handled properly, they will end up accumulating in log files. This can create issues for the Operations team when they want to manage higher environments.

We will discuss here some steps for both Mule EE and API gateway runtimes. The latest Mule EE runtime is 3.8.2. Older versions (< 3.8.X) have Mule EE runtime and API gateway runtime separately.

API gateway runtime is a feature to support the sharing of connectors across the projects deployed in the domain. However, there's a lot more to it than that. To know more about API gateway runtime, check out this documentation from MuleSoft. Mule EE 3.8.2 runtime has unified both API gateway and Mule core runtime.

Avoid Exception Stack Traces With Mule 3.8 Runtime

Configure the logException attribute as false in the catch-exception-strategy block. By default, logException is set as true, which enables Mule to print an exception stack trace. 

Here's an example snippet:

<catch-exception-strategy doc:name="Catch Exception Strategy" logException="false">
 <logger level="ERROR" doc:name="exceptionLogger" message="Exception message is ... #[exception.message]"/>
  <set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
  <set-payload value="{  errors: {     errorCode: #[message.inboundProperties.'http.status'],     errorMessage: #[flowVars.exceptionMessage]  }  }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>

Avoid Exception Stack Traces With Mule API Gateway Runtime

Mule API gateway runtime coupled with 3.7.x or Mule runtime (< 3.8.x) does not have the logException attribute defined in its implementation. The solution to avoid stack trace is below.

Define a logger inside the catch-exception-strategy and set the category, along with setting the level to ERROR.

Here's an example snippet:

<catch-exception-strategy doc:name="Catch Exception Strategy">
 <logger level="ERROR" doc:name="exceptionLogger" message="Exception message is ... #[exception.message]" category="com.project.stacktrace"/>
  <set-variable variableName="exceptionMessage" value="#[exception.message]" doc:name="Set exceptionMessage"/>
  <set-payload value="{  errors: {     errorCode: #[message.inboundProperties.'http.status'],     errorMessage: #[flowVars.exceptionMessage]  }  }" doc:name="Set Exception Payload"/>
</catch-exception-strategy>

Configure the below log4j settings in the project's log4j2.xml file as shown below:

<AsyncLogger name="org.mule.exception.CatchMessagingExceptionStrategy" level="FATAL"/>
<AsyncLogger name="com.project.stacktrace" level="ERROR"/>

I have demonstrated the above steps this a video tutorial.

We will continue expanding this example with error handling on Anypoint Studio in the next article.

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:
exception handling ,mule ,api gateway ,integration ,stack traces ,tutorial

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}