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