View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.errors;
12  
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.Collections;
16  import java.util.List;
17  
18  import org.eclipse.jgit.internal.JGitText;
19  
20  /**
21   * An exception detailing multiple reasons for failure.
22   */
23  public class CompoundException extends Exception {
24  	private static final long serialVersionUID = 1L;
25  
26  	private static String format(Collection<Throwable> causes) {
27  		final StringBuilder msg = new StringBuilder();
28  		msg.append(JGitText.get().failureDueToOneOfTheFollowing);
29  		for (Throwable c : causes) {
30  			msg.append("  "); //$NON-NLS-1$
31  			msg.append(c.getMessage());
32  			msg.append("\n"); //$NON-NLS-1$
33  		}
34  		return msg.toString();
35  	}
36  
37  	private final List<Throwable> causeList;
38  
39  	/**
40  	 * Constructs an exception detailing many potential reasons for failure.
41  	 *
42  	 * @param why
43  	 *            Two or more exceptions that may have been the problem.
44  	 */
45  	public CompoundException(Collection<Throwable> why) {
46  		super(format(why));
47  		causeList = Collections.unmodifiableList(new ArrayList<>(why));
48  	}
49  
50  	/**
51  	 * Get the complete list of reasons why this failure happened.
52  	 *
53  	 * @return unmodifiable collection of all possible reasons.
54  	 */
55  	public List<Throwable> getAllCauses() {
56  		return causeList;
57  	}
58  }