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 */
017 package org.apache.camel.processor;
018
019 import org.apache.camel.AsyncCallback;
020 import org.apache.camel.Exchange;
021 import org.apache.camel.Processor;
022 import org.slf4j.Logger;
023 import org.slf4j.LoggerFactory;
024
025 /**
026 * An {@link org.apache.camel.processor.ErrorHandler} used as a safe fallback when
027 * processing by other error handlers such as the {@link org.apache.camel.model.OnExceptionDefinition}.
028 *
029 * @version
030 */
031 public class FatalFallbackErrorHandler extends DelegateAsyncProcessor implements ErrorHandler {
032
033 private static final Logger LOG = LoggerFactory.getLogger(FatalFallbackErrorHandler.class);
034
035 public FatalFallbackErrorHandler(Processor processor) {
036 super(processor);
037 }
038
039 @Override
040 protected boolean processNext(final Exchange exchange, final AsyncCallback callback) {
041 // support the asynchronous routing engine
042 boolean sync = super.processNext(exchange, new AsyncCallback() {
043 public void done(boolean doneSync) {
044 if (exchange.getException() != null) {
045 // an exception occurred during processing onException
046
047 // log detailed error message with as much detail as possible
048 Throwable previous = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
049 String msg = "Exception occurred while trying to handle previously thrown exception on exchangeId: "
050 + exchange.getExchangeId() + " using: [" + processor + "].";
051 if (previous != null) {
052 msg += " The previous and the new exception will be logged in the following.";
053 LOG.error(msg);
054 LOG.error("\\--> Previous exception on exchangeId: " + exchange.getExchangeId() , previous);
055 LOG.error("\\--> New exception on exchangeId: " + exchange.getExchangeId(), exchange.getException());
056 } else {
057 LOG.error(msg);
058 LOG.error("\\--> New exception on exchangeId: " + exchange.getExchangeId(), exchange.getException());
059 }
060
061 // we can propagated that exception to the caught property on the exchange
062 // which will shadow any previously caught exception and cause this new exception
063 // to be visible in the error handler
064 exchange.setProperty(Exchange.EXCEPTION_CAUGHT, exchange.getException());
065
066 // mark this exchange as already been error handler handled (just by having this property)
067 // the false value mean the caught exception will be kept on the exchange, causing the
068 // exception to be propagated back to the caller, and to break out routing
069 exchange.setProperty(Exchange.ERRORHANDLER_HANDLED, false);
070 }
071 callback.done(doneSync);
072 }
073 });
074
075 return sync;
076 }
077
078 @Override
079 public String toString() {
080 return "FatalFallbackErrorHandler[" + processor + "]";
081 }
082 }