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