blob: 4f84c22c4418c978bfa005de9f399ceb41c705aa [file] [log] [blame]
crazybobleeabc4dd02007-02-01 01:44:36 +00001/**
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 */
crazyboblee9bb62022007-02-01 00:06:53 +000016
17package com.google.inject;
18
sberlind9c913a2011-06-26 21:02:54 +000019import static com.google.common.base.Preconditions.checkArgument;
sberlinb7a02b02011-07-08 00:34:16 +000020
21import com.google.common.collect.ImmutableSet;
22import com.google.inject.internal.Errors;
crazybobleee039bac2007-02-02 21:42:09 +000023import com.google.inject.spi.Message;
sberlinb7a02b02011-07-08 00:34:16 +000024
limpbizkitdf98fcd2008-06-14 05:02:15 +000025import java.util.Collection;
crazybobleee039bac2007-02-02 21:42:09 +000026
crazyboblee9bb62022007-02-01 00:06:53 +000027/**
limpbizkit490833f2008-11-02 00:12:39 +000028 * 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.
crazyboblee9bb62022007-02-01 00:06:53 +000030 *
31 * @author crazybob@google.com (Bob Lee)
32 */
crazybobleec1d0c642007-03-07 17:20:22 +000033public class CreationException extends RuntimeException {
crazyboblee9bb62022007-02-01 00:06:53 +000034
limpbizkit490833f2008-11-02 00:12:39 +000035 private final ImmutableSet<Message> messages;
crazybobleee039bac2007-02-02 21:42:09 +000036
limpbizkit490833f2008-11-02 00:12:39 +000037 /** Creates a CreationException containing {@code messages}. */
limpbizkit0aade0c2009-01-26 01:20:21 +000038 public CreationException(Collection<Message> messages) {
limpbizkit490833f2008-11-02 00:12:39 +000039 this.messages = ImmutableSet.copyOf(messages);
40 checkArgument(!this.messages.isEmpty());
41 initCause(Errors.getOnlyCause(this.messages));
crazybobleee039bac2007-02-02 21:42:09 +000042 }
43
limpbizkit490833f2008-11-02 00:12:39 +000044 /** Returns messages for the errors that caused this exception. */
crazybobleee039bac2007-02-02 21:42:09 +000045 public Collection<Message> getErrorMessages() {
limpbizkit490833f2008-11-02 00:12:39 +000046 return messages;
limpbizkit9dc32d42008-06-15 11:29:10 +000047 }
48
49 @Override public String getMessage() {
Sam Berlinf83bda32014-05-03 00:43:10 -040050 return Errors.format("Unable to create injector, see the following errors", messages);
crazyboblee9bb62022007-02-01 00:06:53 +000051 }
limpbizkitb3a8f0b2008-09-05 22:30:40 +000052
53 private static final long serialVersionUID = 0;
crazyboblee9bb62022007-02-01 00:06:53 +000054}