Skip to content

Commit d49e3d7

Browse files
committed
Update exception handlers and messages
1 parent 548ec64 commit d49e3d7

File tree

11 files changed

+36
-39
lines changed

11 files changed

+36
-39
lines changed

src/main/java/com/relogiclabs/json/schema/exception/CommonException.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import com.relogiclabs.json.schema.message.ErrorDetail;
44
import lombok.Getter;
55

6-
public class CommonException extends RuntimeException {
7-
@Getter
8-
private String code;
6+
import java.util.HashMap;
7+
import java.util.Map;
98

10-
public CommonException() {}
9+
public class CommonException extends RuntimeException {
10+
@Getter private final String code;
11+
private Map<String, String> attributes;
1112

1213
public CommonException(String code, String message, Throwable cause) {
1314
super(message, cause);
@@ -25,4 +26,14 @@ public CommonException(ErrorDetail detail, Throwable cause) {
2526
public CommonException(ErrorDetail detail) {
2627
this(detail, null);
2728
}
29+
30+
public String getAttribute(String name) {
31+
if(attributes == null) return null;
32+
return attributes.get(name);
33+
}
34+
35+
public void setAttribute(String name, String value) {
36+
if(attributes == null) attributes = new HashMap<>(5);
37+
attributes.put(name, value);
38+
}
2839
}

src/main/java/com/relogiclabs/json/schema/exception/JsonSchemaException.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,18 @@
1010

1111
@Getter
1212
public class JsonSchemaException extends CommonException {
13-
13+
private final ErrorDetail error;
1414
private final ExpectedDetail expected;
1515
private final ActualDetail actual;
1616

1717
public JsonSchemaException(ErrorDetail error, ExpectedDetail expected, ActualDetail actual) {
18-
super(error.getCode(), format(error, expected, actual));
19-
this.expected = expected;
20-
this.actual = actual;
18+
this(error, expected, actual, null);
2119
}
2220

23-
public JsonSchemaException(ErrorDetail error, ExpectedDetail expected, ActualDetail actual,
24-
Throwable cause) {
21+
public JsonSchemaException(ErrorDetail error, ExpectedDetail expected,
22+
ActualDetail actual, Throwable cause) {
2523
super(error.getCode(), format(error, expected, actual), cause);
24+
this.error = error;
2625
this.expected = expected;
2726
this.actual = actual;
2827
}

src/main/java/com/relogiclabs/json/schema/function/FunctionBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ public FunctionBase(RuntimeContext runtime) {
1515
}
1616

1717
protected boolean failWith(RuntimeException exception) {
18-
return runtime.failWith(exception);
18+
return runtime.getExceptions().failWith(exception);
1919
}
2020
}

src/main/java/com/relogiclabs/json/schema/internal/function/DateTimeAgent.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.relogiclabs.json.schema.message.ExpectedDetail;
1010
import com.relogiclabs.json.schema.time.DateTimeType;
1111
import com.relogiclabs.json.schema.time.JsonDateTime;
12-
import com.relogiclabs.json.schema.tree.RuntimeContext;
1312
import com.relogiclabs.json.schema.type.JFunction;
1413
import com.relogiclabs.json.schema.type.JString;
1514

@@ -30,18 +29,18 @@ public DateTimeAgent(DateTimeParser parser) {
3029
}
3130

3231
public JsonDateTime parse(JFunction function, JString dateTime) {
33-
RuntimeContext runtime = function.getRuntime();
32+
var exceptions = function.getRuntime().getExceptions();
3433
try {
3534
if(parser == null) parser = new DateTimeParser(pattern, type);
3635
return parser.parse(dateTime.getValue());
3736
} catch(DateTimeLexerException ex) {
38-
runtime.failWith(new JsonSchemaException(
37+
exceptions.failWith(new JsonSchemaException(
3938
new ErrorDetail(ex.getCode(), ex.getMessage()),
4039
new ExpectedDetail(function, "a valid ", type, " pattern"),
4140
new ActualDetail(dateTime, "found ", pattern, " that is invalid"),
4241
ex));
4342
} catch(InvalidDateTimeException ex) {
44-
runtime.failWith(new JsonSchemaException(
43+
exceptions.failWith(new JsonSchemaException(
4544
new ErrorDetail(ex.getCode(), ex.getMessage()),
4645
new ExpectedDetail(function, "a valid ", type, " formatted as ", pattern),
4746
new ActualDetail(dateTime, "found ", dateTime, " that is invalid or malformatted"),

src/main/java/com/relogiclabs/json/schema/internal/message/ActualHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public static ActualDetail asValueMismatch(JNode node) {
2222
return new ActualDetail(node, "found ", node.getOutline());
2323
}
2424

25-
public static ActualDetail asInvalidNestedDataType(JNode node) {
26-
return new ActualDetail(node, "found non-composite type ",
27-
getTypeName(node));
25+
public static ActualDetail asInvalidNonCompositeType(JNode node) {
26+
return new ActualDetail(node, "found non-composite ", getTypeName(node),
27+
" value ", node.getOutline());
2828
}
2929

3030
public static ActualDetail asDataTypeArgumentFailed(JNode node) {

src/main/java/com/relogiclabs/json/schema/internal/message/ExpectedHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public static ExpectedDetail asDataTypeMismatch(JDataType dataType) {
2828
return new ExpectedDetail(dataType, "data type ", dataType.toString(true));
2929
}
3030

31-
public static ExpectedDetail asInvalidNestedDataType(JDataType dataType) {
32-
return new ExpectedDetail(dataType, "composite data type");
31+
public static ExpectedDetail asInvalidNonCompositeType(JDataType dataType) {
32+
return new ExpectedDetail(dataType, "a composite value");
3333
}
3434

3535
public static ExpectedDetail asDataTypeArgumentFailed(JDataType dataType) {

src/main/java/com/relogiclabs/json/schema/internal/message/MessageHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public final class MessageHelper {
99
public static final String ValidationFailed = "Validation failed";
1010
public static final String ValueMismatch = "Value mismatch";
1111
public static final String DataTypeMismatch = "Data type mismatch";
12-
public static final String InvalidNestedDataType = "Invalid nested data type";
12+
public static final String InvalidNonCompositeType = "Invalid non-composite value type";
1313
public static final String DataTypeArgumentFailed = "Data type argument failed";
1414
public static final String InvalidNestedFunction = "Invalid nested function operation";
1515
public static final String PropertyNotFound = "Mandatory property not found";

src/main/java/com/relogiclabs/json/schema/tree/FunctionRegistry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,6 @@ private static Object processVarArgs(Parameter parameter, List<JNode> arguments)
224224
}
225225

226226
private boolean failWith(RuntimeException exception) {
227-
return runtime.failWith(exception);
227+
return runtime.getExceptions().failWith(exception);
228228
}
229229
}

src/main/java/com/relogiclabs/json/schema/tree/PragmaRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ public final class PragmaRegistry implements Iterable<Map.Entry<String, JPragma>
2424
private static final String DATE_DATA_TYPE_FORMAT = "DateDataTypeFormat";
2525
private static final String TIME_DATA_TYPE_FORMAT = "TimeDataTypeFormat";
2626

27-
public final Map<String, JPragma> pragmas;
27+
private final Map<String, JPragma> pragmas;
2828

2929
private boolean ignoreUndefinedProperties = PragmaDescriptor
3030
.IGNORE_UNDEFINED_PROPERTIES.getDefaultValue();
3131
private double floatingPointTolerance = PragmaDescriptor
3232
.FLOATING_POINT_TOLERANCE.getDefaultValue();
3333
private boolean ignoreObjectPropertyOrder = PragmaDescriptor
3434
.IGNORE_OBJECT_PROPERTY_ORDER.getDefaultValue();
35-
public String dateDataTypeFormat = PragmaDescriptor
35+
private String dateDataTypeFormat = PragmaDescriptor
3636
.DATE_DATA_TYPE_FORMAT.getDefaultValue();
37-
public String timeDataTypeFormat = PragmaDescriptor
37+
private String timeDataTypeFormat = PragmaDescriptor
3838
.TIME_DATA_TYPE_FORMAT.getDefaultValue();
3939

4040
private DateTimeParser dateTypeParser;

src/main/java/com/relogiclabs/json/schema/tree/RuntimeContext.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.HashMap;
1212
import java.util.Map;
1313
import java.util.UUID;
14-
import java.util.function.Supplier;
1514

1615
import static com.relogiclabs.json.schema.internal.util.StringHelper.concat;
1716
import static com.relogiclabs.json.schema.internal.util.StringHelper.quote;
@@ -54,10 +53,6 @@ public boolean areEqual(double value1, double value2) {
5453
return Math.abs(value1 - value2) < pragmas.getFloatingPointTolerance();
5554
}
5655

57-
public <T> T tryExecute(Supplier<T> function) {
58-
return exceptions.tryExecute(function);
59-
}
60-
6156
public boolean addValidator(FutureValidator validator) {
6257
return validators.put(UUID.randomUUID().toString(), validator) == null;
6358
}
@@ -68,12 +63,6 @@ public boolean invokeValidators() {
6863
return result;
6964
}
7065

71-
public boolean failWith(RuntimeException exception) {
72-
exceptions.tryThrow(exception);
73-
exceptions.tryAdd(exception);
74-
return false;
75-
}
76-
7766
public void clear() {
7867
exceptions.clear();
7968
storage.clear();

src/main/java/com/relogiclabs/json/schema/type/JNode.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.relogiclabs.json.schema.type;
22

3-
import com.relogiclabs.json.schema.exception.CommonException;
43
import com.relogiclabs.json.schema.exception.JsonSchemaException;
54
import com.relogiclabs.json.schema.internal.builder.JNodeBuilder;
65
import com.relogiclabs.json.schema.internal.message.ActualHelper;
@@ -72,7 +71,7 @@ public String getOutline() {
7271
.getMessageFormatter().getOutlineLength());
7372
}
7473

75-
boolean failWith(CommonException exception) {
76-
return getRuntime().failWith(exception);
74+
boolean failWith(RuntimeException exception) {
75+
return getRuntime().getExceptions().failWith(exception);
7776
}
7877
}

0 commit comments

Comments
 (0)