1 /*
2  * Copyright (C) 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.inject;
18 
19 import static com.google.common.base.Preconditions.checkArgument;
20 
21 import com.google.common.collect.ImmutableSet;
22 import com.google.inject.internal.Messages;
23 import com.google.inject.spi.Message;
24 import java.util.Collection;
25 
26 /**
27  * Indicates that there was a runtime failure while providing an instance.
28  *
29  *
30  * @author kevinb@google.com (Kevin Bourrillion)
31  * @author jessewilson@google.com (Jesse Wilson)
32  * @since 2.0
33  */
34 public final class ProvisionException extends RuntimeException {
35 
36   private final ImmutableSet<Message> messages;
37 
38   /** Creates a ProvisionException containing {@code messages}. */
ProvisionException(Iterable<Message> messages)39   public ProvisionException(Iterable<Message> messages) {
40     this.messages = ImmutableSet.copyOf(messages);
41     checkArgument(!this.messages.isEmpty());
42     initCause(Messages.getOnlyCause(this.messages));
43   }
44 
ProvisionException(String message, Throwable cause)45   public ProvisionException(String message, Throwable cause) {
46     super(cause);
47     this.messages = ImmutableSet.of(new Message(message, cause));
48   }
49 
ProvisionException(String message)50   public ProvisionException(String message) {
51     this.messages = ImmutableSet.of(new Message(message));
52   }
53 
54   /** Returns messages for the errors that caused this exception. */
getErrorMessages()55   public Collection<Message> getErrorMessages() {
56     return messages;
57   }
58 
59   @Override
getMessage()60   public String getMessage() {
61     return Messages.formatMessages("Unable to provision, see the following errors", messages);
62   }
63 
64   private static final long serialVersionUID = 0;
65 }
66