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