Home | History | Annotate | Download | only in inject
      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.Errors;
     23 import com.google.inject.spi.Message;
     24 
     25 import java.util.Collection;
     26 
     27 /**
     28  * Indicates that there was a runtime failure while providing an instance.
     29  *
     30  * @author kevinb (at) google.com (Kevin Bourrillion)
     31  * @author jessewilson (at) 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}. */
     39   public ProvisionException(Iterable<Message> messages) {
     40     this.messages = ImmutableSet.copyOf(messages);
     41     checkArgument(!this.messages.isEmpty());
     42     initCause(Errors.getOnlyCause(this.messages));
     43   }
     44 
     45   public ProvisionException(String message, Throwable cause) {
     46     super(cause);
     47     this.messages = ImmutableSet.of(new Message(message, cause));
     48   }
     49 
     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. */
     55   public Collection<Message> getErrorMessages() {
     56     return messages;
     57   }
     58 
     59   @Override public String getMessage() {
     60     return Errors.format("Unable to provision, see the following errors", messages);
     61   }
     62 
     63   private static final long serialVersionUID = 0;
     64 }
     65