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