blob: b3275c2f1ac6b8253a75efa843af01b878e924d7 [file] [log] [blame]
Jonathan Hart54b83e82016-03-26 20:37:20 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart54b83e82016-03-26 20:37:20 -07003 *
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
17package org.onosproject.net.config;
18
19/**
20 * Indicates a field of a configuration was invalid.
21 */
22public class InvalidFieldException extends RuntimeException {
23
24 private final String field;
25 private final String reason;
26
27 /**
28 * Creates a new invalid field exception about a given field.
29 *
30 * @param field field name
31 * @param reason reason the field is invalid
32 */
33 public InvalidFieldException(String field, String reason) {
34 super(message(field, reason));
35 this.field = field;
36 this.reason = reason;
37 }
38
39 /**
40 * Creates a new invalid field exception about a given field.
41 *
42 * @param field field name
43 * @param cause throwable that occurred while trying to validate field
44 */
45 public InvalidFieldException(String field, Throwable cause) {
46 super(message(field, cause.getMessage()));
47 this.field = field;
48 this.reason = cause.getMessage();
49 }
50
51 /**
52 * Returns the field name.
53 *
54 * @return field name
55 */
56 public String field() {
57 return field;
58 }
59
60 /**
61 * Returns the reason the field failed to validate.
62 *
63 * @return reason
64 */
65 public String reason() {
66 return reason;
67 }
68
69 private static String message(String field, String reason) {
70 return "Field \"" + field + "\" is invalid: " + reason;
71 }
72
73}