001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.impl;
018
019import org.apache.camel.Exchange;
020import org.apache.camel.Processor;
021import org.apache.camel.spi.ExceptionHandler;
022import org.apache.camel.spi.UnitOfWork;
023import org.apache.camel.support.LoggingExceptionHandler;
024import org.apache.camel.util.UnitOfWorkHelper;
025
026/**
027 * An {@link ExceptionHandler} that uses the {@link DefaultConsumer} to
028 * process the caused exception to send the message into the Camel routing engine
029 * which allows to let the routing engine handle the exception.
030 * <p/>
031 * An endpoint can be configured with <tt>consumer.bridgeErrorHandler=true</tt> in the URI
032 * to enable this {@link BridgeExceptionHandlerToErrorHandler} on the consumer.
033 * The consumer must extend the {@link DefaultConsumer}, to support this, if not an
034 * {@link IllegalArgumentException} is thrown upon startup.
035 * <p/>
036 * <b>Notice:</b> When using this bridging error handler, then interceptors, onCompletions
037 * does <b>not</b> apply. The {@link Exchange} is processed directly by the Camel
038 * error handler, and does not allow prior actions such as interceptors, onCompletion
039 * to take action.
040 */
041public class BridgeExceptionHandlerToErrorHandler implements ExceptionHandler {
042
043    private final LoggingExceptionHandler fallback;
044    private final DefaultConsumer consumer;
045    private final Processor bridge;
046
047    public BridgeExceptionHandlerToErrorHandler(DefaultConsumer consumer) {
048        this.consumer = consumer;
049        this.fallback = new LoggingExceptionHandler(consumer.getEndpoint().getCamelContext(), consumer.getClass());
050        this.bridge = consumer.getProcessor();
051    }
052
053    @Override
054    public void handleException(Throwable exception) {
055        handleException(null, exception);
056    }
057
058    @Override
059    public void handleException(String message, Throwable exception) {
060        handleException(message, null, exception);
061    }
062
063    @Override
064    public void handleException(String message, Exchange exchange, Throwable exception) {
065        if (exchange == null) {
066            exchange = consumer.getEndpoint().createExchange();
067        }
068
069        // set the caused exception
070        exchange.setException(exception);
071        // and the message
072        exchange.getIn().setBody(message);
073        // and mark as redelivery exhausted as we cannot do redeliveries
074        exchange.setProperty(Exchange.REDELIVERY_EXHAUSTED, Boolean.TRUE);
075
076        // wrap in UoW
077        UnitOfWork uow = null;
078        try {
079            uow = consumer.createUoW(exchange);
080            bridge.process(exchange);
081        } catch (Exception e) {
082            fallback.handleException("Error handling exception " + exception.getMessage(), exchange, e);
083        } finally {
084            UnitOfWorkHelper.doneUow(uow, exchange);
085        }
086    }
087}