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 * Cause will be first selected exception from result, if applicable. All exceptions are added as suppressed as well.
33 *
34 * @param result The version result at the point the exception occurred, may be {@code null}.
35 */
36 public VersionResolutionException(VersionResult result) {
37 super(getMessage(result), getFirstCause(result));
38 if (result != null) {
39 result.getExceptions().forEach(this::addSuppressed);
40 }
41 this.result = result;
42 }
43
44 private static String getMessage(VersionResult result) {
45 StringBuilder buffer = new StringBuilder(256);
46 buffer.append("Failed to resolve version");
47 if (result != null) {
48 buffer.append(" for ").append(result.getRequest().getArtifact());
49 if (!result.getExceptions().isEmpty()) {
50 buffer.append(": ")
51 .append(result.getExceptions().iterator().next().getMessage());
52 }
53 }
54 return buffer.toString();
55 }
56
57 private static Throwable getFirstCause(VersionResult result) {
58 Throwable cause = null;
59 if (result != null && !result.getExceptions().isEmpty()) {
60 cause = result.getExceptions().get(0);
61 }
62 return cause;
63 }
64
65 /**
66 * Creates a new exception with the specified result and detail message.
67 * Cause will be first selected exception from result, if applicable. All exceptions are added as suppressed as well.
68 *
69 * @param result The version result at the point the exception occurred, may be {@code null}.
70 * @param message The detail message, may be {@code null}.
71 */
72 public VersionResolutionException(VersionResult result, String message) {
73 super(message, getFirstCause(result));
74 if (result != null) {
75 result.getExceptions().forEach(this::addSuppressed);
76 }
77 this.result = result;
78 }
79
80 /**
81 * Creates a new exception with the specified result, detail message and cause.
82 * All exceptions are added as suppressed as well.
83 *
84 * @param result The version 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 VersionResolutionException(VersionResult result, String message, Throwable cause) {
89 super(message, cause);
90 if (result != null) {
91 result.getExceptions().forEach(this::addSuppressed);
92 }
93 this.result = result;
94 }
95
96 /**
97 * Gets the version result at the point the exception occurred. Despite being incomplete, callers might want to use
98 * this result to fail gracefully and continue their operation with whatever interim data has been gathered.
99 *
100 * @return The version result or {@code null} if unknown.
101 */
102 public VersionResult getResult() {
103 return result;
104 }
105 }