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