blob: 819c7cfcfa98ef5b0fe018668c0074bc9d7d2ab2 [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
crazybobleee039bac2007-02-02 21:42:09 +000019import com.google.inject.spi.Message;
kevinb9na2915a92007-02-28 06:20:30 +000020import java.util.ArrayList;
crazybobleee039bac2007-02-02 21:42:09 +000021import java.util.Collection;
22import java.util.Collections;
kevinb9na2915a92007-02-28 06:20:30 +000023import java.util.Comparator;
kevinb9n6a565c72007-02-11 01:58:33 +000024import java.util.Formatter;
crazyboblee0789b192007-02-13 02:43:28 +000025import java.util.List;
crazybobleee039bac2007-02-02 21:42:09 +000026
crazyboblee9bb62022007-02-01 00:06:53 +000027/**
kevinb9na2915a92007-02-28 06:20:30 +000028 * Thrown when errors occur while creating a {@link Injector}. Includes a list
kevinb9na99dca72007-02-11 04:48:57 +000029 * of encountered errors. Typically, a client should catch this exception, log
30 * it, and stop execution.
crazyboblee9bb62022007-02-01 00:06:53 +000031 *
32 * @author crazybob@google.com (Bob Lee)
33 */
crazybobleec1d0c642007-03-07 17:20:22 +000034public class CreationException extends RuntimeException {
crazyboblee9bb62022007-02-01 00:06:53 +000035
crazybobleed9d16a02007-09-09 21:18:48 +000036 final List<? extends Message> errorMessages;
crazybobleee039bac2007-02-02 21:42:09 +000037
38 /**
39 * Constructs a new exception for the given errors.
40 */
crazybobleed9d16a02007-09-09 21:18:48 +000041 public CreationException(Collection<? extends Message> errorMessages) {
crazyboblee62fcdde2007-02-03 02:10:13 +000042 super();
crazyboblee0789b192007-02-13 02:43:28 +000043
44 // Sort the messages by source.
45 this.errorMessages = new ArrayList<Message>(errorMessages);
46 Collections.sort(this.errorMessages, new Comparator<Message>() {
47 public int compare(Message a, Message b) {
48 return a.getSourceString().compareTo(b.getSourceString());
49 }
50 });
crazybobleee039bac2007-02-02 21:42:09 +000051 }
52
crazyboblee62fcdde2007-02-03 02:10:13 +000053 public String getMessage() {
54 return createErrorMessage(errorMessages);
55 }
56
crazybobleed9d16a02007-09-09 21:18:48 +000057 private static String createErrorMessage(
58 Collection<? extends Message> errorMessages) {
kevinb9n6a565c72007-02-11 01:58:33 +000059 Formatter fmt = new Formatter().format("Guice configuration errors:%n%n");
crazybobleee039bac2007-02-02 21:42:09 +000060 int index = 1;
61 for (Message errorMessage : errorMessages) {
crazyboblee0789b192007-02-13 02:43:28 +000062 fmt.format("%s) Error at %s:%n", index++, errorMessage.getSourceString())
kevinb9n6a565c72007-02-11 01:58:33 +000063 .format(" %s%n%n", errorMessage.getMessage());
crazybobleee039bac2007-02-02 21:42:09 +000064 }
crazyboblee0bfdbc62007-02-20 21:47:19 +000065 return fmt.format("%s error[s]", errorMessages.size()).toString();
crazybobleee039bac2007-02-02 21:42:09 +000066 }
67
68 /**
69 * Gets the error messages which resulted in this exception.
70 */
71 public Collection<Message> getErrorMessages() {
72 return Collections.unmodifiableCollection(errorMessages);
crazyboblee9bb62022007-02-01 00:06:53 +000073 }
crazyboblee9bb62022007-02-01 00:06:53 +000074}