'Handling custom exception in camel-context.xml
I come to you because I need your help.
In my file camel-context.xml I have the call to a stored procedure which obtains the information of a user. What I need to solve is that:
If the stored procedure does not return data, a UserNotFound exception is sent (I already have the class) with the http code 404.
I've been searching how to do it, but can't find anything that can help me. I'm just getting started with this framework, so thank you very much in advance for your help.
Here my code:
<route id="getUser" streamCache="false">
<from uri="direct:getUser"/>
<to uri="sql-stored:[dbo].[usp_get_user](VARCHAR ${headers.userId})?dataSource=#primarydatasource" />
<setBody>
<simple>${body['#result-set-1']}</simple>
</setBody>
<marshal> <custom ref="userObject" /> </marshal>
<unmarshal>
<custom ref="userObject"/>
</unmarshal>
</route>
Solution 1:[1]
You can add an onException clause to handle the PL-SQL error:
<routes>
<route id="getUser" streamCache="false">
<from uri="direct:getUser"/>
<onException>
<exception>some.package.UserNotFound</exception>
<handled>
<constant>true</constant>
</handled>
<bean ref="someExceptionHandlerBean" method="handleUserNotFound"/>
<!-- remaining endpoints declaration -->
</onException>
<to uri="sql-stored:[dbo].[usp_get_user](VARCHAR ${headers.userId})?dataSource=#primarydatasource" />
<setBody>
<simple>${body['#result-set-1']}</simple>
</setBody>
<marshal> <custom ref="userObject" /> </marshal>
<unmarshal>
<custom ref="userObject"/>
</unmarshal>
</route>
</routes>
The someExceptionHandlerBean would be a POJO bean defining a #handleUserNotFound method within which you would return an error specific wrapper, in your case it would be some kind of HTTP response object with proper 404 status.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tmarwen |
