Reputation: 167
I have a route with an error handler:
<route errorHandlerRef="magentoCustomerErrorHandler" id="customers.route2">
...
<to id="_to1" uri="http4://{{magento.api.url}}customer/"/>
</route>
And in my error handler, I call a processor in onRedelivery
<bean class="br.com.company.ProcessorError" id="myErrorProcessor"/>
<bean class="org.apache.camel.builder.DeadLetterChannelBuilder" id="magentoCustomerErrorHandler">
<property name="deadLetterUri" value="activemq:magento:customers:DQL"/>
<property name="onRedelivery" ref="myErrorProcessor"/>
<property name="redeliveryPolicy" ref="myRedeliveryPolicyConfig"/>
</bean>
In the error processor, I try to get the message returned by the API but I only get the message generated by the camel.
ErrorProcessor class:
public void process(Exchange exchange) throws Exception {
Exception cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
exchange.getIn().setHeader("FailedBecause", cause.getMessage());
}
API Response:
{"messages":{"error":[{"code":500,"message":"Token doesn't exist or is expired."}]}}
Expected Message:
Token doesn't exist or is expired
Returned message:
HTTP operation failed invoking http://myurl.com/api/rest/customer/ with statusCode: 500
Upvotes: 1
Views: 3978
Reputation: 167
My error here is the type of the Exception that I used.
To I get the body returned by the REST, I need to use the HttpOperationFailedException.
ErrorProcessor class
public void process(Exchange exchange) throws Exception {
HttpOperationFailedException cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, HttpOperationFailedException.class);
exchange.getIn().setHeader("FailedBecause", cause.getMessage());
exchange.getIn().setHeader("ResponseBody", cause.getResponseBody());
}
Upvotes: 1
Reputation: 66
Camel treats any HTTP response code other than 200 as a failure when making http calls, so you need to setThrowExceptionOnFailure
to false to instruct camel route to ignore http response code and just return whatever body it receives.
Here is the Java DSL example:
getContext().getEndpoint(endpoint, HttpEndpoint.class).setThrowExceptionOnFailure(false);
Make sure the endpoint is the host:port only without any http path, it does a exact match.
Refer to: http://camel.apache.org/http.html, look for throwExceptionOnFailure
Please note, if you set it to false, then camel won't go to exception handling route, it returns as normal, and you need to handle the error response outside the camel route. I think this way is better because you have full response from the service you are calling, and you can do your error handling based on the actual failure code/reason from the response.
Upvotes: 0
Reputation: 1196
Try to enable the useOriginalMessage
option :
<bean class="org.apache.camel.builder.DeadLetterChannelBuilder" id="magentoCustomerErrorHandler">
<property name="deadLetterUri" value="activemq:magento:customers:DQL"/>
<property name="onRedelivery" ref="myErrorProcessor"/>
<property name="useOriginalMessage" value="true"/>
<property name="redeliveryPolicy" ref="myRedeliveryPolicyConfig"/>
Upvotes: 0