blob: ba78de49ac6fa90b7a76ef140e2ca960185bdbf9 [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;
limpbizkitf530b252008-05-27 23:03:42 +000020
21import java.util.*;
crazybobleee039bac2007-02-02 21:42:09 +000022
crazyboblee9bb62022007-02-01 00:06:53 +000023/**
kevinb9na2915a92007-02-28 06:20:30 +000024 * Thrown when errors occur while creating a {@link Injector}. Includes a list
kevinb9na99dca72007-02-11 04:48:57 +000025 * of encountered errors. Typically, a client should catch this exception, log
26 * it, and stop execution.
crazyboblee9bb62022007-02-01 00:06:53 +000027 *
28 * @author crazybob@google.com (Bob Lee)
29 */
crazybobleec1d0c642007-03-07 17:20:22 +000030public class CreationException extends RuntimeException {
crazyboblee9bb62022007-02-01 00:06:53 +000031
crazybobleed9d16a02007-09-09 21:18:48 +000032 final List<? extends Message> errorMessages;
crazybobleee039bac2007-02-02 21:42:09 +000033
34 /**
35 * Constructs a new exception for the given errors.
36 */
crazybobleed9d16a02007-09-09 21:18:48 +000037 public CreationException(Collection<? extends Message> errorMessages) {
crazyboblee62fcdde2007-02-03 02:10:13 +000038 super();
crazyboblee0789b192007-02-13 02:43:28 +000039
40 // Sort the messages by source.
41 this.errorMessages = new ArrayList<Message>(errorMessages);
42 Collections.sort(this.errorMessages, new Comparator<Message>() {
43 public int compare(Message a, Message b) {
limpbizkitf530b252008-05-27 23:03:42 +000044 return a.getSource().compareTo(b.getSource());
crazyboblee0789b192007-02-13 02:43:28 +000045 }
46 });
crazybobleee039bac2007-02-02 21:42:09 +000047 }
48
crazyboblee62fcdde2007-02-03 02:10:13 +000049 public String getMessage() {
50 return createErrorMessage(errorMessages);
51 }
52
crazybobleed9d16a02007-09-09 21:18:48 +000053 private static String createErrorMessage(
54 Collection<? extends Message> errorMessages) {
kevinb9n6a565c72007-02-11 01:58:33 +000055 Formatter fmt = new Formatter().format("Guice configuration errors:%n%n");
crazybobleee039bac2007-02-02 21:42:09 +000056 int index = 1;
57 for (Message errorMessage : errorMessages) {
limpbizkitf530b252008-05-27 23:03:42 +000058 fmt.format("%s) Error at %s:%n", index++, errorMessage.getSource())
kevinb9n6a565c72007-02-11 01:58:33 +000059 .format(" %s%n%n", errorMessage.getMessage());
crazybobleee039bac2007-02-02 21:42:09 +000060 }
crazyboblee0bfdbc62007-02-20 21:47:19 +000061 return fmt.format("%s error[s]", errorMessages.size()).toString();
crazybobleee039bac2007-02-02 21:42:09 +000062 }
63
64 /**
65 * Gets the error messages which resulted in this exception.
66 */
67 public Collection<Message> getErrorMessages() {
68 return Collections.unmodifiableCollection(errorMessages);
crazyboblee9bb62022007-02-01 00:06:53 +000069 }
crazyboblee9bb62022007-02-01 00:06:53 +000070}