001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.collection;
020
021import org.eclipse.aether.RepositoryException;
022
023/**
024 * Thrown in case of bad artifact descriptors, version ranges or other issues encountered during calculation of the
025 * dependency graph.
026 */
027public class DependencyCollectionException extends RepositoryException {
028
029    private final transient CollectResult result;
030
031    /**
032     * Creates a new exception with the specified result.
033     *
034     * @param result The collection result at the point the exception occurred, may be {@code null}.
035     */
036    public DependencyCollectionException(CollectResult result) {
037        super("Failed to collect dependencies for " + getSource(result), getCause(result));
038        this.result = result;
039    }
040
041    /**
042     * Creates a new exception with the specified result and detail message.
043     *
044     * @param result The collection result at the point the exception occurred, may be {@code null}.
045     * @param message The detail message, may be {@code null}.
046     */
047    public DependencyCollectionException(CollectResult result, String message) {
048        super(message, getCause(result));
049        this.result = result;
050    }
051
052    /**
053     * Creates a new exception with the specified result, detail message and cause.
054     *
055     * @param result The collection result at the point the exception occurred, may be {@code null}.
056     * @param message The detail message, may be {@code null}.
057     * @param cause The exception that caused this one, may be {@code null}.
058     */
059    public DependencyCollectionException(CollectResult result, String message, Throwable cause) {
060        super(message, cause);
061        this.result = result;
062    }
063
064    /**
065     * Gets the collection result at the point the exception occurred. Despite being incomplete, callers might want to
066     * use this result to fail gracefully and continue their operation with whatever interim data has been gathered.
067     *
068     * @return The collection result or {@code null} if unknown.
069     */
070    public CollectResult getResult() {
071        return result;
072    }
073
074    private static String getSource(CollectResult result) {
075        if (result == null) {
076            return "";
077        }
078
079        CollectRequest request = result.getRequest();
080        if (request.getRoot() != null) {
081            return request.getRoot().toString();
082        }
083        if (request.getRootArtifact() != null) {
084            return request.getRootArtifact().toString();
085        }
086
087        return request.getDependencies().toString();
088    }
089
090    private static Throwable getCause(CollectResult result) {
091        Throwable cause = null;
092        if (result != null && !result.getExceptions().isEmpty()) {
093            cause = result.getExceptions().get(0);
094        }
095        return cause;
096    }
097}