1 /*
2 * Copyright 2007 Kasper B. Graversen
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 package org.supercsv.exception;
17
18 import org.supercsv.cellprocessor.ift.CellProcessor;
19 import org.supercsv.util.CsvContext;
20
21 /**
22 * Exception thrown when CellProcessor execution fails (typically due to invalid input) - constraint validating
23 * CellProcessors should throw {@link SuperCsvConstraintViolationException} for constraint validation failures.
24 *
25 * @author James Bassett
26 * @since 2.0.0
27 */
28 public class SuperCsvCellProcessorException extends SuperCsvException {
29
30 private static final long serialVersionUID = 1L;
31
32 private final CellProcessor processor;
33
34 /**
35 * Constructs a new <tt>SuperCsvCellProcessorException</tt>.
36 *
37 * @param msg
38 * the exception message
39 * @param context
40 * the CSV context
41 * @param processor
42 * the cell processor that was executing
43 */
44 public SuperCsvCellProcessorException(final String msg, final CsvContext context, final CellProcessor processor) {
45 super(msg, context);
46 this.processor = processor;
47 }
48
49 /**
50 * Constructs a new <tt>SuperCsvCellProcessorException</tt>.
51 *
52 * @param msg
53 * the exception message
54 * @param context
55 * the CSV context
56 * @param processor
57 * the cell processor that was executing
58 * @param t
59 * the nested exception
60 */
61 public SuperCsvCellProcessorException(final String msg, final CsvContext context, final CellProcessor processor,
62 final Throwable t) {
63 super(msg, context, t);
64 this.processor = processor;
65 }
66
67 /**
68 * Constructs a new <tt>SuperCsvCellProcessorException</tt> to indicate that the value received by a CellProcessor
69 * wasn't of the correct type.
70 *
71 * @param expectedType
72 * the expected type
73 * @param actualValue
74 * the value received by the CellProcessor
75 * @param context
76 * the CSV context
77 * @param processor
78 * the cell processor that was executing
79 */
80 public SuperCsvCellProcessorException(final Class<?> expectedType, final Object actualValue,
81 final CsvContext context, final CellProcessor processor) {
82 super(getUnexpectedTypeMessage(expectedType, actualValue), context);
83 this.processor = processor;
84 }
85
86 /**
87 * Assembles the exception message when the value received by a CellProcessor isn't of the correct type.
88 *
89 * @param expectedType
90 * the expected type
91 * @param actualValue
92 * the value received by the CellProcessor
93 * @return the message
94 * @throws NullPointerException
95 * if expectedType is null
96 */
97 private static String getUnexpectedTypeMessage(final Class<?> expectedType, final Object actualValue) {
98 if( expectedType == null ) {
99 throw new NullPointerException("expectedType should not be null");
100 }
101 String expectedClassName = expectedType.getName();
102 String actualClassName = (actualValue != null) ? actualValue.getClass().getName() : "null";
103 return String.format("the input value should be of type %s but is %s", expectedClassName, actualClassName);
104 }
105
106 /**
107 * Gets the processor that was executing.
108 *
109 * @return the processor that was executing
110 */
111 public CellProcessor getProcessor() {
112 return processor;
113 }
114
115 /**
116 * Returns the String representation of this exception.
117 */
118 @Override
119 public String toString() {
120 return String.format("%s: %s%nprocessor=%s%ncontext=%s", getClass().getName(), getMessage(), processor,
121 getCsvContext());
122 }
123
124 }