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 unparseable or unresolvable version range.
026 */
027public class VersionRangeResolutionException
028    extends RepositoryException
029{
030
031    private final transient VersionRangeResult result;
032
033    /**
034     * Creates a new exception with the specified result.
035     * 
036     * @param result The version range result at the point the exception occurred, may be {@code null}.
037     */
038    public VersionRangeResolutionException( VersionRangeResult result )
039    {
040        super( getMessage( result ), getCause( result ) );
041        this.result = result;
042    }
043
044    private static String getMessage( VersionRangeResult result )
045    {
046        StringBuilder buffer = new StringBuilder( 256 );
047        buffer.append( "Failed to resolve version range" );
048        if ( result != null )
049        {
050            buffer.append( " for " ).append( result.getRequest().getArtifact() );
051            if ( !result.getExceptions().isEmpty() )
052            {
053                buffer.append( ": " ).append( result.getExceptions().iterator().next().getMessage() );
054            }
055        }
056        return buffer.toString();
057    }
058
059    private static Throwable getCause( VersionRangeResult result )
060    {
061        Throwable cause = null;
062        if ( result != null && !result.getExceptions().isEmpty() )
063        {
064            cause = result.getExceptions().get( 0 );
065        }
066        return cause;
067    }
068
069    /**
070     * Creates a new exception with the specified result and detail message.
071     * 
072     * @param result The version range result at the point the exception occurred, may be {@code null}.
073     * @param message The detail message, may be {@code null}.
074     */
075    public VersionRangeResolutionException( VersionRangeResult result, String message )
076    {
077        super( message );
078        this.result = result;
079    }
080
081    /**
082     * Creates a new exception with the specified result, detail message and cause.
083     * 
084     * @param result The version range result at the point the exception occurred, may be {@code null}.
085     * @param message The detail message, may be {@code null}.
086     * @param cause The exception that caused this one, may be {@code null}.
087     */
088    public VersionRangeResolutionException( VersionRangeResult result, String message, Throwable cause )
089    {
090        super( message, cause );
091        this.result = result;
092    }
093
094    /**
095     * Gets the version range result at the point the exception occurred. Despite being incomplete, callers might want
096     * to use this result to fail gracefully and continue their operation with whatever interim data has been gathered.
097     * 
098     * @return The version range result or {@code null} if unknown.
099     */
100    public VersionRangeResult getResult()
101    {
102        return result;
103    }
104
105}