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