Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding basic error type support for output units. #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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