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.resolution;
020
021import org.eclipse.aether.RepositoryException;
022
023/**
024 * Thrown in case of an unreadable or unresolvable artifact descriptor.
025 */
026public class ArtifactDescriptorException extends RepositoryException {
027
028    private final transient ArtifactDescriptorResult result;
029
030    /**
031     * Creates a new exception with the specified result.
032     *
033     * @param result The descriptor result at the point the exception occurred, may be {@code null}.
034     */
035    public ArtifactDescriptorException(ArtifactDescriptorResult result) {
036        super(
037                "Failed to read artifact descriptor"
038                        + (result != null ? " for " + result.getRequest().getArtifact() : ""),
039                getCause(result));
040        this.result = result;
041    }
042
043    /**
044     * Creates a new exception with the specified result and detail message.
045     *
046     * @param result The descriptor result at the point the exception occurred, may be {@code null}.
047     * @param message The detail message, may be {@code null}.
048     */
049    public ArtifactDescriptorException(ArtifactDescriptorResult result, String message) {
050        super(message, getCause(result));
051        this.result = result;
052    }
053
054    /**
055     * Creates a new exception with the specified result, detail message and cause.
056     *
057     * @param result The descriptor result at the point the exception occurred, may be {@code null}.
058     * @param message The detail message, may be {@code null}.
059     * @param cause The exception that caused this one, may be {@code null}.
060     */
061    public ArtifactDescriptorException(ArtifactDescriptorResult result, String message, Throwable cause) {
062        super(message, cause);
063        this.result = result;
064    }
065
066    /**
067     * Gets the descriptor result at the point the exception occurred. Despite being incomplete, callers might want to
068     * use this result to fail gracefully and continue their operation with whatever interim data has been gathered.
069     *
070     * @return The descriptor result or {@code null} if unknown.
071     */
072    public ArtifactDescriptorResult getResult() {
073        return result;
074    }
075
076    private static Throwable getCause(ArtifactDescriptorResult result) {
077        Throwable cause = null;
078        if (result != null && !result.getExceptions().isEmpty()) {
079            cause = result.getExceptions().get(0);
080        }
081        return cause;
082    }
083}