Skip to content

Commit c8ba171

Browse files
authored
Merge pull request #260 from metafacture/includeCallerLocationInMacroException
Include caller location and text in macro execution exception.
2 parents 80aad27 + 36cd3da commit c8ba171

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

metafix/src/main/java/org/metafacture/metafix/Metafix.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,13 @@ public void putMacro(final String name, final RecordTransformer macro) {
171171
}
172172

173173
public RecordTransformer getMacro(final String name) {
174-
return macros.get(name);
174+
final RecordTransformer macro = macros.get(name);
175+
176+
if (macro != null) {
177+
macro.setParentExceptionMessageFrom(recordTransformer);
178+
}
179+
180+
return macro;
175181
}
176182

177183
public List<Expression> getExpressions() {

metafix/src/main/java/org/metafacture/metafix/RecordTransformer.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public class RecordTransformer { // checkstyle-disable-line ClassFanOutComplexit
6767
private final Metafix metafix;
6868
private final RecordTransformer parent;
6969

70+
private String parentExceptionMessage;
7071
private Supplier<String> currentMessageSupplier;
7172

7273
private enum Vars {
@@ -242,10 +243,10 @@ private MetafactureException tryRun(final Runnable runnable) { // checkstyle-dis
242243
return e; // TODO: Add nesting information?
243244
}
244245
catch (final IllegalStateException | NumberFormatException e) {
245-
return new FixExecutionException(currentMessageSupplier.get(), e);
246+
return new FixExecutionException(getCurrentExceptionMessage(), e);
246247
}
247248
catch (final RuntimeException e) { // checkstyle-disable-line IllegalCatch
248-
final MetafactureException exception = new FixProcessException(currentMessageSupplier.get(), e);
249+
final MetafactureException exception = new FixProcessException(getCurrentExceptionMessage(), e);
249250

250251
if (metafix.getStrictnessHandlesProcessExceptions()) {
251252
return exception;
@@ -258,6 +259,24 @@ private MetafactureException tryRun(final Runnable runnable) { // checkstyle-dis
258259
return null;
259260
}
260261

262+
private String getCurrentExceptionMessage() {
263+
final StringBuilder sb = new StringBuilder();
264+
265+
if (parentExceptionMessage != null) {
266+
sb.append(parentExceptionMessage);
267+
sb.append(" -> ");
268+
}
269+
270+
sb.append(currentMessageSupplier.get());
271+
272+
return sb.toString();
273+
}
274+
275+
/*package-private*/ void setParentExceptionMessageFrom(final RecordTransformer parentTransformer) {
276+
parentExceptionMessage = parentTransformer != null && parentTransformer.currentMessageSupplier != null ?
277+
parentTransformer.currentMessageSupplier.get() : null;
278+
}
279+
261280
private String executionExceptionMessage(final Expression expression) {
262281
return executionExceptionMessage(expression, expression.eResource());
263282
}

metafix/src/test/java/org/metafacture/metafix/MetafixRecordTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,64 @@ public void shouldCallNestedMacro() {
21552155
);
21562156
}
21572157

2158+
@Test
2159+
public void shouldIncludeCallerLocationAndTextForExceptionInMacro() {
2160+
final String format = "Error while executing Fix expression (at FILE, line %d): %s";
2161+
2162+
final String text1 = "call_macro('test')";
2163+
final String text2 = "append('animals', ' is cool')";
2164+
final String message = String.format(format, 4, text1) + " -> " + String.format(format, 2, text2);
2165+
2166+
MetafixTestHelpers.assertThrows(FixExecutionException.class, s -> s.replaceAll("file:/.+?\\.fix", "FILE"), message, () ->
2167+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2168+
"do put_macro('test')",
2169+
text2,
2170+
"end",
2171+
text1
2172+
),
2173+
i -> {
2174+
i.startRecord("1");
2175+
i.startEntity("animals");
2176+
i.literal("1", "dog");
2177+
i.literal("2", "cat");
2178+
i.literal("3", "zebra");
2179+
i.endEntity();
2180+
i.endRecord();
2181+
},
2182+
o -> {
2183+
}
2184+
)
2185+
);
2186+
}
2187+
2188+
@Test
2189+
public void shouldIncludeCallerLocationAndTextForExceptionInIncludedMacro() {
2190+
final String format = "Error while executing Fix expression (at FILE, line %d): %s";
2191+
2192+
final String text1 = "call_macro('test')";
2193+
final String text2 = "append('animals', ' is cool')";
2194+
final String message = String.format(format, 2, text1) + " -> " + String.format(format, 2, text2);
2195+
2196+
MetafixTestHelpers.assertThrows(FixExecutionException.class, s -> s.replaceAll("file:/.+?\\.fix", "FILE"), message, () ->
2197+
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
2198+
"include('src/test/resources/org/metafacture/metafix/fixes/macro.fix')",
2199+
text1
2200+
),
2201+
i -> {
2202+
i.startRecord("1");
2203+
i.startEntity("animals");
2204+
i.literal("1", "dog");
2205+
i.literal("2", "cat");
2206+
i.literal("3", "zebra");
2207+
i.endEntity();
2208+
i.endRecord();
2209+
},
2210+
o -> {
2211+
}
2212+
)
2213+
);
2214+
}
2215+
21582216
@Test
21592217
public void reject() {
21602218
MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList(
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
do put_macro("test")
2+
append('animals', ' is cool')
3+
end

0 commit comments

Comments
 (0)