View Javadoc
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      private final List<? extends Throwable> throwables;
33  
34      private MultiRuntimeException(String message, List<? extends Throwable> throwables) {
35          super(message);
36          this.throwables = throwables;
37          for (Throwable throwable : throwables) {
38              addSuppressed(throwable);
39          }
40      }
41  
42      /**
43       * Returns the list of throwables that are wrapped in this exception.
44       *
45       * @return The list of throwables, never {@code null}.
46       */
47      public List<? extends Throwable> getThrowables() {
48          return throwables;
49      }
50  
51      /**
52       * Helper method that receives a (non-null) message and (non-null) list of throwable, and following happens:
53       * <ul>
54       *     <li>if list is empty - nothing</li>
55       *     <li>if list not empty - {@link MultiRuntimeException} is thrown wrapping all elements</li>
56       * </ul>
57       */
58      public static void mayThrow(String message, List<? extends Throwable> throwables) {
59          requireNonNull(message);
60          requireNonNull(throwables);
61  
62          if (!throwables.isEmpty()) {
63              throw new MultiRuntimeException(message, throwables);
64          }
65      }
66  }