I have come to the conclusion that a route-level onException clause or a doTry/doCatch is the only way to deal with exceptions behind an enricher.
Camel ErrorHandler useOriginalMessage Attribute
-
11-07-2023 - |
Question
I've been using Camel (currently version 2.12.2) for a little over a year now and have run into an interesting situation with one of my web applicaitons (a few hundred routes). To overcome this issue, I want to use a special ErrorHandler that returns the original body if an exception is thrown in specific enriched routes. Here is a little sample context I put together to illustrate what I want to happen:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- use for enriched routes so they pass the original to the global onException if exception is encountered -->
<camel:errorHandler id='enrichmentErrorHandler' type="DefaultErrorHandler" useOriginalMessage="true"/>
<camel:endpoint camelContextId="TestErrorHandlerContext" id="TestServletEndpoint" uri="servlet:///order"/>
<bean id="testEx" class="java.io.IOException"/>
<camelContext id="TestErrorHandlerContext" xmlns="http://camel.apache.org/schema/spring" xmlns:oas="http://www.verizonwireless.com/oas" trace="true">
<onException>
<exception>java.io.IOException</exception>
<handled><constant>true</constant></handled>
<log message="logging"/>
</onException>
<route id="startRoute">
<from uri="TestServletEndpoint"/>
<choice>
<when>
<simple>${body} == 'hello'</simple>
<setBody><constant>world</constant></setBody>
<!-- use new body -->
<throwException ref="testEx"/>
</when>
<otherwise>
<enrich uri="direct:enrichRoute"/>
</otherwise>
</choice>
</route>
<route id="enrichRoute" errorHandlerRef="enrichmentErrorHandler">
<from uri="direct:enrichRoute"/>
<setBody><constant>somethingElse</constant></setBody>
<!-- use original body -->
<throwException ref="testEx"/>
</route>
</camelContext>
</beans>
When I send 'hello', I get back 'world' -> expected
When I send 'something', I get back 'sometingElse' -> unexpected
My thought process tells me that the enrichRoute should use the original body in the since it is referencing the enrichmentErrorHandler. What about my expectation is incorrect?
I am aware that I can have route-level onException blocks to return the original body, but it would be cleaner if I could get the error handler reference working since I'd have to change about 40 or so route definitions.
Solution