1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether;
20
21 import java.util.List;
22
23 import static java.util.Objects.requireNonNull;
24
25 /**
26 * Runtime exception to be thrown when multiple actions were executed and one or more failed. To be used when no
27 * fallback on resolver side is needed or is possible.
28 *
29 * @since 1.9.0
30 */
31 public final class MultiRuntimeException extends RuntimeException {
32 /** The wrapped throwables. */
33 private final List<? extends Throwable> throwables;
34
35 private MultiRuntimeException(String message, List<? extends Throwable> throwables) {
36 super(message);
37 this.throwables = throwables;
38 for (Throwable throwable : throwables) {
39 addSuppressed(throwable);
40 }
41 }
42
43 /**
44 * Returns the list of throwables that are wrapped in this exception.
45 *
46 * @return The list of throwables, never {@code null}.
47 */
48 public List<? extends Throwable> getThrowables() {
49 return throwables;
50 }
51
52 /**
53 * Helper method that receives a (non-null) message and (non-null) list of throwable, and following happens:
54 * <ul>
55 * <li>if list is empty - nothing</li>
56 * <li>if list not empty - {@link MultiRuntimeException} is thrown wrapping all elements</li>
57 * </ul>
58 *
59 * @param message The exception message to use if a {@link MultiRuntimeException} is thrown, must not be
60 * {@code null}.
61 * @param throwables The throwables to wrap, must not be {@code null}.
62 */
63 public static void mayThrow(String message, List<? extends Throwable> throwables) {
64 requireNonNull(message);
65 requireNonNull(throwables);
66
67 if (!throwables.isEmpty()) {
68 throw new MultiRuntimeException(message, throwables);
69 }
70 }
71 }