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.CamelContext;
020import org.apache.camel.Route;
021import org.apache.camel.spi.RouteError;
022
023public class DefaultRouteError implements RouteError {
024    private final RouteError.Phase phase;
025    private final Throwable throwable;
026
027    public DefaultRouteError(Phase phase, Throwable throwable) {
028        this.phase = phase;
029        this.throwable = throwable;
030    }
031
032    @Override
033    public Phase getPhase() {
034        return phase;
035    }
036
037    @Override
038    public Throwable getException() {
039        return throwable;
040    }
041
042    @Override
043    public String toString() {
044        return "DefaultRouteError{"
045            + "phase=" + phase
046            + ", throwable=" + throwable
047            + '}';
048    }
049
050    // ***********************************
051    // Helpers
052    // ***********************************
053
054    public static void set(CamelContext context, String routeId, RouteError.Phase phase, Throwable throwable) {
055        Route route = context.getRoute(routeId);
056        if (route != null) {
057            route.getRouteContext().setLastError(new DefaultRouteError(phase, throwable));
058        }
059    }
060
061    public static void reset(CamelContext context, String routeId) {
062        Route route = context.getRoute(routeId);
063        if (route != null) {
064            route.getRouteContext().setLastError(null);
065        }
066    }
067}