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;
018
019import org.apache.camel.util.URISupport;
020
021/**
022 * Exception when failing to create a {@link org.apache.camel.Route}.
023 */
024public class FailedToCreateRouteException extends CamelException {
025    private static final long serialVersionUID = 1L;
026    private final String routeId;
027
028    public FailedToCreateRouteException(String routeId, String route, Throwable cause) {
029        super("Failed to create route " + routeId + ": " + getRouteMessage(route) + " because of " + getExceptionMessage(cause), cause);
030        this.routeId = routeId;
031    }
032
033    public FailedToCreateRouteException(String routeId, String route, String at, Throwable cause) {
034        super("Failed to create route " + routeId + " at: >>> " + at + " <<< in route: " + getRouteMessage(route) + " because of " + getExceptionMessage(cause), cause);
035        this.routeId = routeId;
036    }
037
038    public String getRouteId() {
039        return routeId;
040    }
041    
042    protected static String getExceptionMessage(Throwable cause) {
043        return cause.getMessage() != null ? cause.getMessage() : cause.getClass().getSimpleName();
044    }
045
046    protected static String getRouteMessage(String route) {
047        // ensure to sanitize uri's in the route so we do not show sensitive information such as passwords
048        route = URISupport.sanitizeUri(route);
049
050        // cut the route after 60 chars so it won't be too big in the message
051        // users just need to be able to identify the route so they know where to look
052        if (route.length() > 60) {
053            return route.substring(0, 60) + "...";
054        } else {
055            return route;
056        }
057    }
058
059}