1

I want to handle errors depending on the http code response.

I would also like to know how to enable *throwExceptionOnFailure* on my route. For example, if the response code is 500x, send the message to the queue "redmine_errors"

UPDATE 4:

my blueprint after add exception from answer @fg78nc (don't work)

<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
        http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
        http://cxf.apache.org/blueprint/jaxws http://cxf.apache.org/schemas/blueprint/jaxws.xsd
        http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
       ">

<bean id="tfsToRedmineMapper"
    class="com.stackabuse.example.TfsToRedmineMapper" />

<bean id="myBean" class="com.stackabuse.example.MyBean" />

<camelContext
    xmlns="http://camel.apache.org/schema/blueprint">

<onException>
    <exception>org.apache.camel.http.common.HttpOperationFailedException
    </exception>
    <onWhen>
        <method ref="myBean" method="parseException" />
    </onWhen>
    <handled>
        <constant>true</constant>
    </handled>
    <to uri="log:redmine_errors" />
</onException>
    <route>
        <from uri="jetty:http://0.0.0.0:8082/test" />
        <inOnly uri="activemq://from_tfs" />
    </route>
    <route>
        <from uri="activemq://from_tfs" />
        <process ref="tfsToRedmineMapper" />
        <to uri="activemq://for_redmine" />
    </route>
    <route>
        <from uri="activemq://for_redmine" />
        <setHeader headerName="Content-Type">
            <constant>application/json; charset=utf-8</constant>
        </setHeader>
        <setHeader headerName="X-Redmine-API-Key">
            <constant>my_redmine_api_token</constant>
        </setHeader>
        <toD uri="${header.url}" />
    </route>

ERROR: 2019-02-15 09:35:12,103 | ERROR | mix-7.0.1/deploy | BlueprintCamelContext | 40 - org.apache.camel.camel-blueprint - 2.16.5 | Error occurred during starting Camel: CamelContext(camel-32) due Failed to create route route48 at: >>> OnException[null When[bean{} -> []] -> [To[activemq://redmine_errors]]] <<< in route: Route(route48)[[From[jetty:http://0.0.0.0:8082/test]] -> [On... because of org.apache.camel.http.common.HttpOperationFailedException org.apache.camel.FailedToCreateRouteException: Failed to create route route48 at: >>> OnException[null When[bean{} -> []] -> [To[activemq://redmine_errors]]] <<< in route: Route(route48)[[From[jetty:http://0.0.0.0:8082/test]] -> [On... because of org.apache.camel.http.common.HttpOperationFailedException

enter image description here

enter image description here

3
  • 1
    You should be able to append ?throwExceptionOnFailure=false to the end of the URI to invoke. This will prevent Camel from throwing an exception but instead you can check the response code in the header via .when(header(Exchange.HTTP_RESPONSE_CODE)).isGreaterThanOrEqualTo(400))... The actual response body will be available in the in-message body Commented Feb 12, 2019 at 12:55
  • You should move onException outside of the route, i.e. keep it on the CamelContext level, not the route level, so it will apply to all routes. Commented Feb 12, 2019 at 23:54
  • I did as you said, but now I get errors: org.osgi.service.blueprint.container.ComponentDefinitionException: Unable to validate xml ... Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'onException'. One of '{"http://camel.apache.org/schema/blueprint":route}' is expected. Commented Feb 13, 2019 at 7:45

1 Answer 1

1

Unfortunately, Camel does not set correctly Http status code. Solution below is a little bit convoluted, but it works. It can also be solved within XML with the simple language predicate, but somehow it did not work for me, so I used Java for predicate.

Blueprint :

 <bean id="myBean" class="com.example.MyBean" />

 <onException>
     <exception>org.apache.camel.http.common.HttpOperationFailedException</exception>
      <onWhen>
         <method ref="myBean" method="parseException" />
      </onWhen>
      <handled>
         <constant>true</constant>
      </handled>
      <to uri="jms:redmine_errors"/>
 </onException>

Java :

       package com.example;

       public class MyBean {

       public boolean parseException(Exchange exchange){
              return exchange.getProperty("CamelExceptionCaught")
                             .toString().contains("statusCode: 500");
            }
       }
Sign up to request clarification or add additional context in comments.

13 Comments

2019-02-12 09:41:36,704 | ERROR | mix-7.0.1/deploy | BlueprintCamelContext | 40 - org.apache.camel.camel-blueprint - 2.16.5 | Error occurred during starting Camel: CamelContext(camel-40) due Failed to create route route117 at: >>> OnException[null When[simple{${header.CamelHttpResponseCode} == 500} -> [To[jms:redmine_errors]]] -> []] <<< in route: Route(route117)[[From[activemq://for_redmine]] -> [OnExcepti... because of org.apache.camel.http.common.HttpOperationFailedException
@D.Batmanov I have updated my answer, please check if it works for you. It did for me.
if I add onException block i have error(bundle does not start): Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'onException'. One of '{"camel.apache.org/schema/blueprint":route}' is expected. I will add my full blueprint above( my development machine is intranet(maybe this is important?))
Did you add it within CamelContext element, but outside of route element?
Please put onException block above route blocks.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.