1 /*
2 * Copyright 2009-2010 Steve Chaloner
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 be.objectify.led.validation;
17
18 /**
19 * Indicates validation has failed.
20 *
21 * @author Steve Chaloner
22 */
23 public class ValidationException extends RuntimeException
24 {
25 private String fieldName;
26
27 private String reason;
28
29 /**
30 * Initialises a new instance with the target field name and reason for the validation failure.
31 *
32 * @param fieldName the field name
33 * @param reason the reason
34 */
35 public ValidationException(String fieldName,
36 String reason)
37 {
38 super(String.format("Validation failed on field [%s] because [%s]",
39 fieldName,
40 reason));
41 this.fieldName = fieldName;
42 this.reason = reason;
43 }
44
45 /**
46 * Initialises a new instance with the target field name, reason for the validation failure and
47 * a root cause.
48 *
49 * @param fieldName the field name
50 * @param reason the reason
51 * @param cause an associated exception
52 */
53 public ValidationException(String fieldName,
54 String reason,
55 Throwable cause)
56 {
57 super(String.format("Validation failed on field [%s] because [%s]",
58 fieldName,
59 reason),
60 cause);
61 this.fieldName = fieldName;
62 this.reason = reason;
63 }
64
65
66 public String getFieldName()
67 {
68 return fieldName;
69 }
70
71 public String getReason()
72 {
73 return reason;
74 }
75 }