001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.collection; 020 021import org.eclipse.aether.RepositoryException; 022 023/** 024 * Thrown in case of bad artifact descriptors, version ranges or other issues encountered during calculation of the 025 * dependency graph. 026 */ 027public class DependencyCollectionException extends RepositoryException { 028 029 private final transient CollectResult result; 030 031 /** 032 * Creates a new exception with the specified result. 033 * Cause will be first selected exception from result, if applicable. All exceptions are added as suppressed as well. 034 * 035 * @param result The collection result at the point the exception occurred, may be {@code null}. 036 */ 037 public DependencyCollectionException(CollectResult result) { 038 super("Failed to collect dependencies for " + getSource(result), getFirstCause(result)); 039 if (result != null) { 040 result.getExceptions().forEach(this::addSuppressed); 041 } 042 this.result = result; 043 } 044 045 /** 046 * Creates a new exception with the specified result and detail message. 047 * Cause will be first selected exception from result, if applicable. All exceptions are added as suppressed as well. 048 * 049 * @param result The collection result at the point the exception occurred, may be {@code null}. 050 * @param message The detail message, may be {@code null}. 051 */ 052 public DependencyCollectionException(CollectResult result, String message) { 053 super(message, getFirstCause(result)); 054 if (result != null) { 055 result.getExceptions().forEach(this::addSuppressed); 056 } 057 this.result = result; 058 } 059 060 /** 061 * Creates a new exception with the specified result, detail message and cause. 062 * All exceptions are added as suppressed as well. 063 * 064 * @param result The collection result at the point the exception occurred, may be {@code null}. 065 * @param message The detail message, may be {@code null}. 066 * @param cause The exception that caused this one, may be {@code null}. 067 */ 068 public DependencyCollectionException(CollectResult result, String message, Throwable cause) { 069 super(message, cause); 070 if (result != null) { 071 result.getExceptions().forEach(this::addSuppressed); 072 } 073 this.result = result; 074 } 075 076 /** 077 * Gets the collection result at the point the exception occurred. Despite being incomplete, callers might want to 078 * use this result to fail gracefully and continue their operation with whatever interim data has been gathered. 079 * 080 * @return The collection result or {@code null} if unknown. 081 */ 082 public CollectResult getResult() { 083 return result; 084 } 085 086 private static String getSource(CollectResult result) { 087 if (result == null) { 088 return ""; 089 } 090 091 CollectRequest request = result.getRequest(); 092 if (request.getRoot() != null) { 093 return request.getRoot().toString(); 094 } 095 if (request.getRootArtifact() != null) { 096 return request.getRootArtifact().toString(); 097 } 098 099 return request.getDependencies().toString(); 100 } 101 102 private static Throwable getFirstCause(CollectResult result) { 103 Throwable cause = null; 104 if (result != null && !result.getExceptions().isEmpty()) { 105 cause = result.getExceptions().get(0); 106 } 107 return cause; 108 } 109}