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

D. Batmanov
  • 39
  • 1
  • 8
  • 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 – Roman Vottner Feb 12 '19 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. – fg78nc Feb 12 '19 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.` – D. Batmanov Feb 13 '19 at 07:45

1 Answers1

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");
            }
       }
fg78nc
  • 4,774
  • 3
  • 19
  • 32
  • 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 Feb 12 '19 at 06:43
  • @D.Batmanov I have updated my answer, please check if it works for you. It did for me. – fg78nc Feb 13 '19 at 19:19
  • 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 '{"http://camel.apache.org/schema/blueprint":route}' is expected. I will add my full blueprint above( my development machine is intranet(maybe this is important?)) – D. Batmanov Feb 14 '19 at 07:36
  • Did you add it within CamelContext element, but outside of route element? – fg78nc Feb 14 '19 at 13:13
  • Please put onException block above route blocks. – fg78nc Feb 14 '19 at 14:47
  • Could you please upload this project, so I can test? – fg78nc Feb 15 '19 at 13:39
  • i use servicemix, for run need install jetty: in karaf condole - feature:install camel-jetty9 – D. Batmanov Feb 18 '19 at 10:53
  • https://github.com/fg78nc/HttpOperationFail_test.git - Try to execute HttpExceptionTest in src/test/java. Test uses your blueprint file, I slightly modified it with mocks to avoid deployment to OSGI environment. HTTP call to external resource throws 404, which is caught and handled by rerouting to another route. – fg78nc Feb 18 '19 at 15:40
  • [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.8.1:test (default-test) on project stackabuse-example-blueprint: Error while executing forked tests. -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.8.1:test (default-test) on project stackabuse-example-blueprint: Error while executing forked tests. Caused by: org.apache.maven.plugin.MojoExecutionException: Error while executing forked tests. – D. Batmanov Feb 19 '19 at 07:14
  • Try to execute that particular test from IDE itself. – fg78nc Feb 19 '19 at 14:34
  • add this dependency ` org.apache.camel camel-http 2.17.0 ` and run `mvn test` from command line https://pasteboard.co/I227C8H.png – fg78nc Feb 20 '19 at 12:29
  • Are you using the same pom.xml that I have put on github? Did you made any modifications to project? – fg78nc Feb 20 '19 at 16:09
  • what did you put on github, changed nothing – D. Batmanov Feb 21 '19 at 06:01