Hi we have a route in camel for an HTTP Rest endpoint that returns JSON with
the following exception handling to convert exceptions in the route to JSON

<onException>
    <onException>
    
<exception>org.springframework.security.access.AccessDeniedException</exception>
        <handled>
            <constant>true</constant>
        </handled> 
        <setHeader headerName="CamelHttpResponseCode">
            <constant>403</constant>
        </setHeader>
        <setHeader headerName="Content-Type">
            <constant>application/json</constant>
        </setHeader>
        <process ref="oAuth2TokenRevokeProcessor"/>
            <transform>
                <simple>{"error":"${exception.message}"}</simple>
            </transform>        
      </onException>            
</onException>

This was previously working, but has stopped working since we upgraded to
camel 2.9.0 - we now get this exception 

org.apache.camel.language.simple.types.SimpleIllegalSyntaxException:
functionEnd has no matching start token at location 31
{"error":"${exception.message}"}
                               *

        at
org.apache.camel.language.simple.SimpleExpressionParser.parseExpression(SimpleExpressionParser.java:50)[115:org.apache.camel.camel-core:2.9.0.fuse-beta-7-042]
        at

It seems camel no longer likes the closing curly bracket. 

So instead i tried using SPEL:

<transform>
    <spel>{"error":"#{exception.message}"}</spel>
</transform>

But this fails with 

org.apache.camel.ExpressionEvaluationException:
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos
10): Field or property 'message' cannot be found on null

Ie SPEL cannot seem to see the exception even though the documentation for
SPEL in camel says it should be avaliable.

In the end we have it working like this:

<transform>
     <simple>${exception.message}</simple>
</transform>
<transform>
      <spel>{"error":"#{request.body}"}</spel>
</transform> 

But this seems overkill for such a simple case - is there a simpler way to
do this?

Should the simple expression fail the way it does with a closing curly
bracket - this was OK before and obviously causes issues in JSON

Why does SPEL think the exception is null?


--
View this message in context: 
http://camel.465427.n5.nabble.com/Translating-route-exceptions-to-JSON-with-Simple-Expression-Language-or-SPEL-tp5696933.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Reply via email to