crazyboblee | abc4dd0 | 2007-02-01 01:44:36 +0000 | [diff] [blame] | 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 | */ |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 16 | |
| 17 | package com.google.inject; |
| 18 | |
sberlin | d9c913a | 2011-06-26 21:02:54 +0000 | [diff] [blame] | 19 | import static com.google.common.base.Preconditions.checkArgument; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 20 | |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.inject.internal.Errors; |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 23 | import com.google.inject.spi.Message; |
sberlin | b7a02b0 | 2011-07-08 00:34:16 +0000 | [diff] [blame] | 24 | |
limpbizkit | df98fcd | 2008-06-14 05:02:15 +0000 | [diff] [blame] | 25 | import java.util.Collection; |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 26 | |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 27 | /** |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 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. |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 30 | * |
| 31 | * @author crazybob@google.com (Bob Lee) |
| 32 | */ |
crazyboblee | c1d0c64 | 2007-03-07 17:20:22 +0000 | [diff] [blame] | 33 | public class CreationException extends RuntimeException { |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 34 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 35 | private final ImmutableSet<Message> messages; |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 36 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 37 | /** Creates a CreationException containing {@code messages}. */ |
limpbizkit | 0aade0c | 2009-01-26 01:20:21 +0000 | [diff] [blame] | 38 | public CreationException(Collection<Message> messages) { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 39 | this.messages = ImmutableSet.copyOf(messages); |
| 40 | checkArgument(!this.messages.isEmpty()); |
| 41 | initCause(Errors.getOnlyCause(this.messages)); |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 42 | } |
| 43 | |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 44 | /** Returns messages for the errors that caused this exception. */ |
crazyboblee | e039bac | 2007-02-02 21:42:09 +0000 | [diff] [blame] | 45 | public Collection<Message> getErrorMessages() { |
limpbizkit | 490833f | 2008-11-02 00:12:39 +0000 | [diff] [blame] | 46 | return messages; |
limpbizkit | 9dc32d4 | 2008-06-15 11:29:10 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override public String getMessage() { |
Sam Berlin | f83bda3 | 2014-05-03 00:43:10 -0400 | [diff] [blame] | 50 | return Errors.format("Unable to create injector, see the following errors", messages); |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 51 | } |
limpbizkit | b3a8f0b | 2008-09-05 22:30:40 +0000 | [diff] [blame] | 52 | |
| 53 | private static final long serialVersionUID = 0; |
crazyboblee | 9bb6202 | 2007-02-01 00:06:53 +0000 | [diff] [blame] | 54 | } |