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