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.resolution;
20  
21  import org.eclipse.aether.RepositoryException;
22  
23  /**
24   * Thrown in case of an unreadable or unresolvable artifact descriptor.
25   */
26  public class ArtifactDescriptorException extends RepositoryException {
27  
28      private final transient ArtifactDescriptorResult result;
29  
30      /**
31       * Creates a new exception with the specified result.
32       *
33       * @param result The descriptor result at the point the exception occurred, may be {@code null}.
34       */
35      public ArtifactDescriptorException(ArtifactDescriptorResult result) {
36          super(
37                  "Failed to read artifact descriptor"
38                          + (result != null ? " for " + result.getRequest().getArtifact() : ""),
39                  getCause(result));
40          this.result = result;
41      }
42  
43      /**
44       * Creates a new exception with the specified result and detail message.
45       *
46       * @param result The descriptor result at the point the exception occurred, may be {@code null}.
47       * @param message The detail message, may be {@code null}.
48       */
49      public ArtifactDescriptorException(ArtifactDescriptorResult result, String message) {
50          super(message, getCause(result));
51          this.result = result;
52      }
53  
54      /**
55       * Creates a new exception with the specified result, detail message and cause.
56       *
57       * @param result The descriptor result at the point the exception occurred, may be {@code null}.
58       * @param message The detail message, may be {@code null}.
59       * @param cause The exception that caused this one, may be {@code null}.
60       */
61      public ArtifactDescriptorException(ArtifactDescriptorResult result, String message, Throwable cause) {
62          super(message, cause);
63          this.result = result;
64      }
65  
66      /**
67       * Gets the descriptor result at the point the exception occurred. Despite being incomplete, callers might want to
68       * use this result to fail gracefully and continue their operation with whatever interim data has been gathered.
69       *
70       * @return The descriptor result or {@code null} if unknown.
71       */
72      public ArtifactDescriptorResult getResult() {
73          return result;
74      }
75  
76      private static Throwable getCause(ArtifactDescriptorResult result) {
77          Throwable cause = null;
78          if (result != null && !result.getExceptions().isEmpty()) {
79              cause = result.getExceptions().get(0);
80          }
81          return cause;
82      }
83  }