use of org.apache.camel.model.OnExceptionDefinition in project camel by apache.
the class ErrorHandlerBuilderRef method createErrorHandler.
private ErrorHandlerBuilder createErrorHandler(RouteContext routeContext) {
ErrorHandlerBuilder handler = (ErrorHandlerBuilder) lookupErrorHandlerBuilder(routeContext, getRef());
ObjectHelper.notNull(handler, "error handler '" + ref + "'");
// configure if the handler support transacted
supportTransacted = handler.supportTransacted();
List<OnExceptionDefinition> list = getErrorHandlers(routeContext);
if (list != null) {
for (OnExceptionDefinition exceptionType : list) {
handler.addErrorHandlers(routeContext, exceptionType);
}
}
return handler;
}
use of org.apache.camel.model.OnExceptionDefinition in project camel by apache.
the class ErrorHandlerBuilderSupport method configure.
public void configure(RouteContext routeContext, ErrorHandler handler) {
if (handler instanceof ErrorHandlerSupport) {
ErrorHandlerSupport handlerSupport = (ErrorHandlerSupport) handler;
List<OnExceptionDefinition> list = onExceptions.get(routeContext);
if (list != null) {
for (OnExceptionDefinition exception : list) {
handlerSupport.addExceptionPolicy(routeContext, exception);
}
}
}
if (handler instanceof RedeliveryErrorHandler) {
boolean original = ((RedeliveryErrorHandler) handler).isUseOriginalMessagePolicy();
if (original) {
// ensure allow original is turned on
routeContext.setAllowUseOriginalMessage(true);
}
}
}
use of org.apache.camel.model.OnExceptionDefinition in project camel by apache.
the class RedeliveryErrorHandler method handleException.
protected void handleException(Exchange exchange, RedeliveryData data, boolean isDeadLetterChannel) {
Exception e = exchange.getException();
// store the original caused exception in a property, so we can restore it later
exchange.setProperty(Exchange.EXCEPTION_CAUGHT, e);
// find the error handler to use (if any)
OnExceptionDefinition exceptionPolicy = getExceptionPolicy(exchange, e);
if (exceptionPolicy != null) {
data.currentRedeliveryPolicy = exceptionPolicy.createRedeliveryPolicy(exchange.getContext(), data.currentRedeliveryPolicy);
data.handledPredicate = exceptionPolicy.getHandledPolicy();
data.continuedPredicate = exceptionPolicy.getContinuedPolicy();
data.retryWhilePredicate = exceptionPolicy.getRetryWhilePolicy();
data.useOriginalInMessage = exceptionPolicy.getUseOriginalMessagePolicy() != null && exceptionPolicy.getUseOriginalMessagePolicy();
// route specific failure handler?
Processor processor = null;
UnitOfWork uow = exchange.getUnitOfWork();
if (uow != null && uow.getRouteContext() != null) {
String routeId = uow.getRouteContext().getRoute().getId();
processor = exceptionPolicy.getErrorHandler(routeId);
} else if (!exceptionPolicy.getErrorHandlers().isEmpty()) {
// note this should really not happen, but we have this code as a fail safe
// to be backwards compatible with the old behavior
log.warn("Cannot determine current route from Exchange with id: {}, will fallback and use first error handler.", exchange.getExchangeId());
processor = exceptionPolicy.getErrorHandlers().iterator().next();
}
if (processor != null) {
data.failureProcessor = processor;
}
// route specific on redelivery?
processor = exceptionPolicy.getOnRedelivery();
if (processor != null) {
data.onRedeliveryProcessor = processor;
}
// route specific on exception occurred?
processor = exceptionPolicy.getOnExceptionOccurred();
if (processor != null) {
data.onExceptionProcessor = processor;
}
}
// only log if not failure handled or not an exhausted unit of work
if (!ExchangeHelper.isFailureHandled(exchange) && !ExchangeHelper.isUnitOfWorkExhausted(exchange)) {
String msg = "Failed delivery for " + ExchangeHelper.logIds(exchange) + ". On delivery attempt: " + data.redeliveryCounter + " caught: " + e;
logFailedDelivery(true, false, false, false, isDeadLetterChannel, exchange, msg, data, e);
}
data.redeliveryCounter = incrementRedeliveryCounter(exchange, e, data);
}
use of org.apache.camel.model.OnExceptionDefinition in project camel by apache.
the class DefaultExceptionPolicyStrategyTest method testClosetMatch2.
public void testClosetMatch2() {
setupPolicies();
OnExceptionDefinition result = strategy.getExceptionPolicy(policies, null, new ClassCastException(""));
assertEquals(type2, result);
result = strategy.getExceptionPolicy(policies, null, new NumberFormatException(""));
assertEquals(type2, result);
result = strategy.getExceptionPolicy(policies, null, new NullPointerException());
assertEquals(type2, result);
}
use of org.apache.camel.model.OnExceptionDefinition in project camel by apache.
the class DefaultExceptionPolicyStrategyTest method testClosetMatch3.
public void testClosetMatch3() {
setupPolicies();
OnExceptionDefinition result = strategy.getExceptionPolicy(policies, null, new ConnectException(""));
assertEquals(type3, result);
result = strategy.getExceptionPolicy(policies, null, new SocketException(""));
assertEquals(type3, result);
result = strategy.getExceptionPolicy(policies, null, new FileNotFoundException());
assertEquals(type3, result);
}
Aggregations