Skip to content

Commit 10522f9

Browse files
committed
Add exception message formatter
1 parent 4f06fdd commit 10522f9

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.relogiclabs.json.schema.message;
2+
3+
import com.relogiclabs.json.schema.tree.Context;
4+
import com.relogiclabs.json.schema.tree.Location;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
8+
import static org.apache.commons.lang3.StringUtils.capitalize;
9+
10+
@Getter
11+
public abstract class MessageFormatter {
12+
private static final String NEWLINE = System.lineSeparator();
13+
14+
private static final String SCHEMA_BASE_EXCEPTION = "Schema Input [%s]: %s";
15+
private static final String JSON_BASE_EXCEPTION = "Json Input [%s]: %s";
16+
private static final String SCHEMA_PARSE_EXCEPTION = "Schema (Line: %s) [%s]: %s";
17+
private static final String JSON_PARSE_EXCEPTION = "Json (Line: %s) [%s]: %s";
18+
19+
public static final MessageFormatter SCHEMA_VALIDATION = new ValidationFormatter(
20+
"Schema (Line: %s) Json (Line: %s) [%s]: %s.",
21+
" %s is expected",
22+
" but %s.");
23+
24+
public static final MessageFormatter SCHEMA_ASSERTION = new AssertionFormatter(
25+
"%s: %s" + NEWLINE,
26+
"Expected (Schema Line: %s): %s" + NEWLINE,
27+
"Actual (Json Line: %s): %s" + NEWLINE);
28+
29+
public static final MessageFormatter JSON_ASSERTION = new AssertionFormatter(
30+
"%s: %s" + NEWLINE,
31+
"Expected (Json Line: %s): %s" + NEWLINE,
32+
"Actual (Json Line: %s): %s" + NEWLINE);
33+
34+
private final String summary;
35+
private final String expected;
36+
private final String actual;
37+
38+
@Setter
39+
private int outlineLength = 200;
40+
41+
protected MessageFormatter(String summary, String expected, String actual) {
42+
this.summary = summary;
43+
this.expected = expected;
44+
this.actual = actual;
45+
}
46+
47+
public abstract String format(ErrorDetail error, ExpectedDetail expected, ActualDetail actual);
48+
49+
private static class ValidationFormatter extends MessageFormatter {
50+
protected ValidationFormatter(String summary, String expected, String actual) {
51+
super(summary, expected, actual);
52+
}
53+
54+
@Override
55+
public String format(ErrorDetail error, ExpectedDetail expected, ActualDetail actual) {
56+
return getSummary().formatted(expected.getLocation(), actual.getLocation(),
57+
error.getCode(), error.getMessage()) +
58+
getExpected().formatted(capitalize(expected.getMessage())) +
59+
getActual().formatted(actual.getMessage());
60+
}
61+
}
62+
63+
private static class AssertionFormatter extends MessageFormatter {
64+
public AssertionFormatter(String summary, String expected, String actual) {
65+
super(summary, expected, actual);
66+
}
67+
68+
@Override
69+
public String format(ErrorDetail error, ExpectedDetail expected, ActualDetail actual) {
70+
return getSummary().formatted(error.getCode(), error.getMessage()) +
71+
getExpected().formatted(expected.getLocation(), expected.getMessage()) +
72+
getActual().formatted(actual.getLocation(), actual.getMessage());
73+
}
74+
}
75+
76+
public static ErrorDetail formatForSchema(String code, String message, Context context) {
77+
return formatForSchema(code, message, context != null? context.getLocation() : null);
78+
}
79+
80+
public static ErrorDetail formatForSchema(String code, String message, Location location) {
81+
return location == null
82+
? createDetail(code, SCHEMA_BASE_EXCEPTION, message)
83+
: createDetail(code, SCHEMA_PARSE_EXCEPTION, message, location);
84+
}
85+
86+
public static ErrorDetail formatForJson(String code, String message, Context context) {
87+
return formatForJson(code, message, context != null? context.getLocation() : null);
88+
}
89+
90+
public static ErrorDetail formatForJson(String code, String message, Location location) {
91+
return location == null
92+
? createDetail(code, JSON_BASE_EXCEPTION, message)
93+
: createDetail(code, JSON_PARSE_EXCEPTION, message, location);
94+
}
95+
96+
private static ErrorDetail createDetail(String code, String format, String message) {
97+
return new ErrorDetail(code, format.formatted(code, message));
98+
}
99+
100+
private static ErrorDetail createDetail(String code, String format, String message,
101+
Location location) {
102+
return new ErrorDetail(code, format.formatted(location, code, message));
103+
}
104+
}

0 commit comments

Comments
 (0)