Skip to content

Commit

Permalink
feat: Adding basic error type support for output units.
Browse files Browse the repository at this point in the history
Signed-off-by: Gerrett <Gerrett.Pape@colorado.edu>
  • Loading branch information
CheesyBoy123 committed May 19, 2024
1 parent 68a54a4 commit cdb9526
Show file tree
Hide file tree
Showing 12 changed files with 156 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, OutputU
obj.setAnnotations(list);
}
break;
case "errorType":
if (member.getValue() instanceof String) {
obj.setErrorType(io.vertx.json.schema.OutputErrorType.valueOf((String)member.getValue()));
}
break;
}
}
}
Expand Down Expand Up @@ -99,5 +104,8 @@ static void toJson(OutputUnit obj, java.util.Map<String, Object> json) {
obj.getAnnotations().forEach(item -> array.add(item.toJson()));
json.put("annotations", array);
}
if (obj.getErrorType() != null) {
json.put("errorType", obj.getErrorType().name());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,24 @@
public final class JsonSchemaValidationException extends Exception {

final private String location;
final private OutputErrorType errorType;

public JsonSchemaValidationException(String message, Throwable cause, String location) {
public JsonSchemaValidationException(String message, Throwable cause, String location, OutputErrorType errorType) {
super(message, cause);
this.location = location;
this.errorType = errorType;
}

public JsonSchemaValidationException(String message, String location, StackTraceElement stackTraceElement) {
this(message, null, location, stackTraceElement);
public JsonSchemaValidationException(String message, String location, OutputErrorType errorType,
StackTraceElement stackTraceElement) {
this(message, null, location, errorType, stackTraceElement);
}

public JsonSchemaValidationException(String message, Throwable cause, String location, StackTraceElement stackTraceElement) {
public JsonSchemaValidationException(String message, Throwable cause, String location, OutputErrorType errorType,
StackTraceElement stackTraceElement) {
super(message, cause, stackTraceElement != null, stackTraceElement != null);
this.location = location;
this.errorType = errorType;
if (stackTraceElement != null) {
setStackTrace(new StackTraceElement[]{
stackTraceElement
Expand All @@ -45,4 +50,12 @@ public JsonSchemaValidationException(String message, Throwable cause, String loc
public String location() {
return location;
}

/**
* @return our best guess on what the validation error type is.
*/
public OutputErrorType errorType() {
return errorType;
}

}
12 changes: 12 additions & 0 deletions src/main/java/io/vertx/json/schema/OutputErrorType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.vertx.json.schema;

import io.vertx.codegen.annotations.VertxGen;

@VertxGen
public enum OutputErrorType {

NONE,
INVALID_VALUE,
MISSING_VALUE

}
18 changes: 16 additions & 2 deletions src/main/java/io/vertx/json/schema/OutputUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class OutputUnit {
private String keywordLocation;
private String instanceLocation;
private String error;
private OutputErrorType errorType = OutputErrorType.NONE;

private List<OutputUnit> errors;
private List<OutputUnit> annotations;
Expand All @@ -44,11 +45,12 @@ public OutputUnit(boolean valid) {
this.valid = valid;
}

public OutputUnit(String instanceLocation, String absoluteKeywordLocation, String keywordLocation, String error) {
public OutputUnit(String instanceLocation, String absoluteKeywordLocation, String keywordLocation, String error, OutputErrorType errorType) {
this.instanceLocation = instanceLocation;
this.absoluteKeywordLocation = absoluteKeywordLocation;
this.keywordLocation = keywordLocation;
this.error = error;
this.errorType = errorType;
}

public Boolean getValid() {
Expand Down Expand Up @@ -132,6 +134,15 @@ public OutputUnit setAnnotations(List<OutputUnit> annotations) {
return this;
}

public OutputErrorType getErrorType() {
return errorType;
}

public OutputUnit setErrorType(OutputErrorType errorType) {
this.errorType = errorType;
return this;
}

public OutputUnit addAnnotation(OutputUnit annotation) {
if (this.annotations == null) {
this.annotations = new ArrayList<>();
Expand Down Expand Up @@ -161,6 +172,7 @@ public void checkValidity() throws JsonSchemaValidationException {
throw new JsonSchemaValidationException(
msg == null ? "JsonSchema Validation error" : msg,
location,
errorType,
// add some information to the stack trace
createStackTraceElement());
} else {
Expand All @@ -173,6 +185,7 @@ public void checkValidity() throws JsonSchemaValidationException {
throw new JsonSchemaValidationException(
msg == null ? "JsonSchema Validation error" : msg,
location,
errorType,
// add some information to the stack trace
createStackTraceElement());
} else {
Expand All @@ -186,6 +199,7 @@ public void checkValidity() throws JsonSchemaValidationException {
error.getError(),
lastException,
location,
errorType,
// add some information to the stack trace
error.createStackTraceElement());
lastException = cause;
Expand All @@ -194,7 +208,7 @@ public void checkValidity() throws JsonSchemaValidationException {
throw lastException;
} else {
// one final wrap as there is extra error message in the unit
throw new JsonSchemaValidationException(msg, lastException, getAbsoluteKeywordLocation());
throw new JsonSchemaValidationException(msg, lastException, getAbsoluteKeywordLocation(), errorType);
}
}
}
Expand Down

0 comments on commit cdb9526

Please sign in to comment.