001package org.eclipse.aether.resolution;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.RepositoryException;
023
024/**
025 * Thrown in case of an unreadable or unresolvable artifact descriptor.
026 */
027public class ArtifactDescriptorException
028    extends RepositoryException
029{
030
031    private final transient ArtifactDescriptorResult result;
032
033    /**
034     * Creates a new exception with the specified result.
035     * 
036     * @param result The descriptor result at the point the exception occurred, may be {@code null}.
037     */
038    public ArtifactDescriptorException( ArtifactDescriptorResult result )
039    {
040        super( "Failed to read artifact descriptor"
041            + ( result != null ? " for " + result.getRequest().getArtifact() : "" ), getCause( result ) );
042        this.result = result;
043    }
044
045    /**
046     * Creates a new exception with the specified result and detail message.
047     * 
048     * @param result The descriptor result at the point the exception occurred, may be {@code null}.
049     * @param message The detail message, may be {@code null}.
050     */
051    public ArtifactDescriptorException( ArtifactDescriptorResult result, String message )
052    {
053        super( message, getCause( result ) );
054        this.result = result;
055    }
056
057    /**
058     * Creates a new exception with the specified result, detail message and cause.
059     * 
060     * @param result The descriptor result at the point the exception occurred, may be {@code null}.
061     * @param message The detail message, may be {@code null}.
062     * @param cause The exception that caused this one, may be {@code null}.
063     */
064    public ArtifactDescriptorException( ArtifactDescriptorResult result, String message, Throwable cause )
065    {
066        super( message, cause );
067        this.result = result;
068    }
069
070    /**
071     * Gets the descriptor result at the point the exception occurred. Despite being incomplete, callers might want to
072     * use this result to fail gracefully and continue their operation with whatever interim data has been gathered.
073     * 
074     * @return The descriptor result or {@code null} if unknown.
075     */
076    public ArtifactDescriptorResult getResult()
077    {
078        return result;
079    }
080
081    private static Throwable getCause( ArtifactDescriptorResult result )
082    {
083        Throwable cause = null;
084        if ( result != null && !result.getExceptions().isEmpty() )
085        {
086            cause = result.getExceptions().get( 0 );
087        }
088        return cause;
089    }
090
091}