View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2010, 2014 Sonatype, Inc.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *    Sonatype, Inc. - initial API and implementation
10   *******************************************************************************/
11  package org.eclipse.aether.collection;
12  
13  import org.eclipse.aether.RepositoryException;
14  
15  /**
16   * Thrown in case of bad artifact descriptors, version ranges or other issues encountered during calculation of the
17   * dependency graph.
18   */
19  public class DependencyCollectionException
20      extends RepositoryException
21  {
22  
23      private final transient CollectResult result;
24  
25      /**
26       * Creates a new exception with the specified result.
27       * 
28       * @param result The collection result at the point the exception occurred, may be {@code null}.
29       */
30      public DependencyCollectionException( CollectResult result )
31      {
32          super( "Failed to collect dependencies for " + getSource( result ), getCause( result ) );
33          this.result = result;
34      }
35  
36      /**
37       * Creates a new exception with the specified result and detail message.
38       * 
39       * @param result The collection result at the point the exception occurred, may be {@code null}.
40       * @param message The detail message, may be {@code null}.
41       */
42      public DependencyCollectionException( CollectResult result, String message )
43      {
44          super( message, getCause( result ) );
45          this.result = result;
46      }
47  
48      /**
49       * Creates a new exception with the specified result, detail message and cause.
50       * 
51       * @param result The collection result at the point the exception occurred, may be {@code null}.
52       * @param message The detail message, may be {@code null}.
53       * @param cause The exception that caused this one, may be {@code null}.
54       */
55      public DependencyCollectionException( CollectResult result, String message, Throwable cause )
56      {
57          super( message, cause );
58          this.result = result;
59      }
60  
61      /**
62       * Gets the collection result at the point the exception occurred. Despite being incomplete, callers might want to
63       * use this result to fail gracefully and continue their operation with whatever interim data has been gathered.
64       * 
65       * @return The collection result or {@code null} if unknown.
66       */
67      public CollectResult getResult()
68      {
69          return result;
70      }
71  
72      private static String getSource( CollectResult result )
73      {
74          if ( result == null )
75          {
76              return "";
77          }
78  
79          CollectRequest request = result.getRequest();
80          if ( request.getRoot() != null )
81          {
82              return request.getRoot().toString();
83          }
84          if ( request.getRootArtifact() != null )
85          {
86              return request.getRootArtifact().toString();
87          }
88  
89          return request.getDependencies().toString();
90      }
91  
92      private static Throwable getCause( CollectResult result )
93      {
94          Throwable cause = null;
95          if ( result != null && !result.getExceptions().isEmpty() )
96          {
97              cause = result.getExceptions().get( 0 );
98          }
99          return cause;
100     }
101 
102 }