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;
020
021import java.util.List;
022
023import static java.util.Objects.requireNonNull;
024
025/**
026 * Runtime exception to be thrown when multiple actions were executed and one or more failed. To be used when no
027 * fallback on resolver side is needed or is possible.
028 *
029 * @since 1.9.0
030 */
031public final class MultiRuntimeException extends RuntimeException {
032    private final List<? extends Throwable> throwables;
033
034    private MultiRuntimeException(String message, List<? extends Throwable> throwables) {
035        super(message);
036        this.throwables = throwables;
037        for (Throwable throwable : throwables) {
038            addSuppressed(throwable);
039        }
040    }
041
042    /**
043     * Returns the list of throwables that are wrapped in this exception.
044     *
045     * @return The list of throwables, never {@code null}.
046     */
047    public List<? extends Throwable> getThrowables() {
048        return throwables;
049    }
050
051    /**
052     * Helper method that receives a (non-null) message and (non-null) list of throwable, and following happens:
053     * <ul>
054     *     <li>if list is empty - nothing</li>
055     *     <li>if list not empty - {@link MultiRuntimeException} is thrown wrapping all elements</li>
056     * </ul>
057     */
058    public static void mayThrow(String message, List<? extends Throwable> throwables) {
059        requireNonNull(message);
060        requireNonNull(throwables);
061
062        if (!throwables.isEmpty()) {
063            throw new MultiRuntimeException(message, throwables);
064        }
065    }
066}