View Javadoc
1   package org.eclipse.aether.collection;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.eclipse.aether.RepositoryException;
23  
24  /**
25   * Thrown in case of bad artifact descriptors, version ranges or other issues encountered during calculation of the
26   * dependency graph.
27   */
28  public class DependencyCollectionException
29      extends RepositoryException
30  {
31  
32      private final transient CollectResult result;
33  
34      /**
35       * Creates a new exception with the specified result.
36       * 
37       * @param result The collection result at the point the exception occurred, may be {@code null}.
38       */
39      public DependencyCollectionException( CollectResult result )
40      {
41          super( "Failed to collect dependencies for " + getSource( result ), getCause( result ) );
42          this.result = result;
43      }
44  
45      /**
46       * Creates a new exception with the specified result and detail message.
47       * 
48       * @param result The collection result at the point the exception occurred, may be {@code null}.
49       * @param message The detail message, may be {@code null}.
50       */
51      public DependencyCollectionException( CollectResult result, String message )
52      {
53          super( message, getCause( result ) );
54          this.result = result;
55      }
56  
57      /**
58       * Creates a new exception with the specified result, detail message and cause.
59       * 
60       * @param result The collection result at the point the exception occurred, may be {@code null}.
61       * @param message The detail message, may be {@code null}.
62       * @param cause The exception that caused this one, may be {@code null}.
63       */
64      public DependencyCollectionException( CollectResult result, String message, Throwable cause )
65      {
66          super( message, cause );
67          this.result = result;
68      }
69  
70      /**
71       * Gets the collection result at the point the exception occurred. Despite being incomplete, callers might want to
72       * use this result to fail gracefully and continue their operation with whatever interim data has been gathered.
73       * 
74       * @return The collection result or {@code null} if unknown.
75       */
76      public CollectResult getResult()
77      {
78          return result;
79      }
80  
81      private static String getSource( CollectResult result )
82      {
83          if ( result == null )
84          {
85              return "";
86          }
87  
88          CollectRequest request = result.getRequest();
89          if ( request.getRoot() != null )
90          {
91              return request.getRoot().toString();
92          }
93          if ( request.getRootArtifact() != null )
94          {
95              return request.getRootArtifact().toString();
96          }
97  
98          return request.getDependencies().toString();
99      }
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 }