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  * Thrown when errors occur while creating a {@link Injector}. Includes a list of encountered
     29  * errors. Clients should catch this exception, log it, and stop execution.
     30  *
     31  * @author crazybob (at) google.com (Bob Lee)
     32  */
     33 public class CreationException extends RuntimeException {
     34 
     35   private final ImmutableSet<Message> messages;
     36 
     37   /** Creates a CreationException containing {@code messages}. */
     38   public CreationException(Collection<Message> messages) {
     39     this.messages = ImmutableSet.copyOf(messages);
     40     checkArgument(!this.messages.isEmpty());
     41     initCause(Errors.getOnlyCause(this.messages));
     42   }
     43 
     44   /** Returns messages for the errors that caused this exception. */
     45   public Collection<Message> getErrorMessages() {
     46     return messages;
     47   }
     48 
     49   @Override public String getMessage() {
     50     return Errors.format("Unable to create injector, see the following errors", messages);
     51   }
     52 
     53   private static final long serialVersionUID = 0;
     54 }
     55