-
Notifications
You must be signed in to change notification settings - Fork 282
/
SchemaException.java
135 lines (109 loc) · 4.78 KB
/
SchemaException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.everit.json.schema;
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
import java.util.Collection;
import java.util.List;
import org.json.JSONObject;
/**
* Thrown by {@link org.everit.json.schema.loader.SchemaLoader#load()} when it encounters
* un-parseable schema JSON definition.
*
* @author erosb
*/
public class SchemaException extends RuntimeException {
private static final long serialVersionUID = 5987489689035036987L;
private static Object typeOfValue(final Object actualValue) {
return actualValue == null ? "null" : actualValue.getClass().getSimpleName();
}
static String buildMessage(String pointer, Class<?> actualType, Class<?> expectedType, Class<?>... furtherExpectedTypes) {
requireNonNull(pointer, "pointer cannot be null");
String actualTypeDescr = actualTypeDescr(actualType);
if (furtherExpectedTypes != null && furtherExpectedTypes.length > 0) {
Class<?>[] allExpecteds = new Class<?>[furtherExpectedTypes.length + 1];
allExpecteds[0] = expectedType;
System.arraycopy(furtherExpectedTypes, 0, allExpecteds, 1, furtherExpectedTypes.length);
return buildMessage(pointer, actualTypeDescr, asList(allExpecteds));
}
return format("%s: expected type: %s, found: %s", pointer,
expectedType.getSimpleName(),
actualTypeDescr);
}
private static String actualTypeDescr(Class<?> actualType) {
return actualType == null ? "null" : actualType.getSimpleName();
}
static String buildMessage(String formattedPointer, String actualTypeDescr, Collection<Class<?>> expectedTypes) {
String fmtExpectedTypes = expectedTypes.stream()
.map(Class::getSimpleName)
.collect(joining(" or "));
return format("%s: expected type is one of %s, found: %s", formattedPointer,
fmtExpectedTypes,
actualTypeDescr);
}
private static String buildMessage(String pointer, Class<?> actualType, Collection<Class<?>> expectedTypes) {
return buildMessage(pointer, actualTypeDescr(actualType), expectedTypes);
}
private static String joinClassNames(final List<Class<?>> expectedTypes) {
return expectedTypes.stream().map(Class::getSimpleName).collect(joining(", "));
}
private final String schemaLocation;
public SchemaException(String schemaLocation, String message) {
super(schemaLocation == null
? "<unknown location>: " + message
: schemaLocation + ": " + message);
this.schemaLocation = schemaLocation;
}
public SchemaException(String schemaLocation, Class<?> actualType, Class<?> expectedType, Class<?>... furtherExpectedTypes) {
super(buildMessage(schemaLocation, actualType, expectedType, furtherExpectedTypes));
this.schemaLocation = schemaLocation;
}
public SchemaException(String schemaLocation, Class<?> actualType, Collection<Class<?>> expectedTypes) {
super(buildMessage(schemaLocation, actualType, expectedTypes));
this.schemaLocation = schemaLocation;
}
public SchemaException(String schemaLocation, Exception cause) {
super(cause.getMessage(), cause);
this.schemaLocation = schemaLocation;
}
@Deprecated
public SchemaException(String message) {
this((String) null, message);
}
@Deprecated
public SchemaException(String key, Class<?> expectedType, Object actualValue) {
this(format("key %s : expected type: %s , found : %s", key, expectedType
.getSimpleName(), typeOfValue(actualValue)));
}
@Deprecated
public SchemaException(String key, List<Class<?>> expectedTypes,
final Object actualValue) {
this(format("key %s: expected type is one of %s, found: %s",
key, joinClassNames(expectedTypes), typeOfValue(actualValue)));
}
@Deprecated
public SchemaException(String message, Throwable cause) {
super(message, cause);
this.schemaLocation = null;
}
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
SchemaException that = (SchemaException) o;
return toString().equals(that.toString());
}
@Override public int hashCode() {
return toString().hashCode();
}
public String getSchemaLocation() {
return schemaLocation;
}
public JSONObject toJSON() {
JSONObject rval = new JSONObject();
rval.put("schemaLocation", schemaLocation);
rval.put("message", getMessage());
return rval;
}
}