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 unresolvable metaversion.
25   */
26  public class VersionResolutionException extends RepositoryException {
27  
28      private final transient VersionResult result;
29  
30      /**
31       * Creates a new exception with the specified result.
32       *
33       * @param result The version result at the point the exception occurred, may be {@code null}.
34       */
35      public VersionResolutionException(VersionResult result) {
36          super(getMessage(result), getCause(result));
37          this.result = result;
38      }
39  
40      private static String getMessage(VersionResult result) {
41          StringBuilder buffer = new StringBuilder(256);
42          buffer.append("Failed to resolve version");
43          if (result != null) {
44              buffer.append(" for ").append(result.getRequest().getArtifact());
45              if (!result.getExceptions().isEmpty()) {
46                  buffer.append(": ")
47                          .append(result.getExceptions().iterator().next().getMessage());
48              }
49          }
50          return buffer.toString();
51      }
52  
53      private static Throwable getCause(VersionResult result) {
54          Throwable cause = null;
55          if (result != null && !result.getExceptions().isEmpty()) {
56              cause = result.getExceptions().get(0);
57          }
58          return cause;
59      }
60  
61      /**
62       * Creates a new exception with the specified result and detail message.
63       *
64       * @param result The version result at the point the exception occurred, may be {@code null}.
65       * @param message The detail message, may be {@code null}.
66       */
67      public VersionResolutionException(VersionResult result, String message) {
68          super(message, getCause(result));
69          this.result = result;
70      }
71  
72      /**
73       * Creates a new exception with the specified result, detail message and cause.
74       *
75       * @param result The version result at the point the exception occurred, may be {@code null}.
76       * @param message The detail message, may be {@code null}.
77       * @param cause The exception that caused this one, may be {@code null}.
78       */
79      public VersionResolutionException(VersionResult result, String message, Throwable cause) {
80          super(message, cause);
81          this.result = result;
82      }
83  
84      /**
85       * Gets the version result at the point the exception occurred. Despite being incomplete, callers might want to use
86       * this result to fail gracefully and continue their operation with whatever interim data has been gathered.
87       *
88       * @return The version result or {@code null} if unknown.
89       */
90      public VersionResult getResult() {
91          return result;
92      }
93  }