Search in sources :

Example 1 with OnExceptionDefinition

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;
}
Also used : OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition)

Example 2 with OnExceptionDefinition

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);
        }
    }
}
Also used : RedeliveryErrorHandler(org.apache.camel.processor.RedeliveryErrorHandler) ErrorHandlerSupport(org.apache.camel.processor.ErrorHandlerSupport) OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition)

Example 3 with OnExceptionDefinition

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);
}
Also used : UnitOfWork(org.apache.camel.spi.UnitOfWork) Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 4 with OnExceptionDefinition

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);
}
Also used : OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition)

Example 5 with OnExceptionDefinition

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);
}
Also used : SocketException(java.net.SocketException) OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition) FileNotFoundException(java.io.FileNotFoundException) ConnectException(java.net.ConnectException)

Aggregations

OnExceptionDefinition (org.apache.camel.model.OnExceptionDefinition)24 IOException (java.io.IOException)9 FileNotFoundException (java.io.FileNotFoundException)6 ConnectException (java.net.ConnectException)5 AlreadyStoppedException (org.apache.camel.AlreadyStoppedException)4 CamelExchangeException (org.apache.camel.CamelExchangeException)4 MalformedURLException (java.net.MalformedURLException)3 SocketException (java.net.SocketException)3 ExchangeTimedOutException (org.apache.camel.ExchangeTimedOutException)3 RuntimeCamelException (org.apache.camel.RuntimeCamelException)3 ValidationException (org.apache.camel.ValidationException)3 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 AsyncCallback (org.apache.camel.AsyncCallback)1 AsyncProcessor (org.apache.camel.AsyncProcessor)1 Processor (org.apache.camel.Processor)1 DefaultRouteNode (org.apache.camel.impl.DefaultRouteNode)1