Skip to content

Commit

Permalink
#3735: show line number
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Dec 23, 2024
1 parent c53993c commit f9d7a3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 22 additions & 1 deletion eo-runtime/src/main/java/org/eolang/PhSafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ private <T> T through(final Action<T> action) {

/**
* Helper, for other methods.
*
* <p>No matter what happens inside the {@code action}, only
* an instance of {@link EOerror.ExError} may be thrown out
* of this method.</p>
*
* @param action The action
* @param suffix The suffix to add to the label
* @param <T> Type of result
Expand All @@ -186,11 +191,27 @@ private <T> T through(final Action<T> action, final String suffix) {
return action.act();
} catch (final EOerror.ExError ex) {
throw new EOerror.ExError(ex, this.label(suffix));
} catch (final RuntimeException | Error ex) {
} catch (final ExAbstract ex) {
throw new EOerror.ExError(
new Data.ToPhi(ex.getMessage()),
this.label(suffix)
);
} catch (final RuntimeException | Error ex) {
final StringBuilder msg = new StringBuilder(0);
final StackTraceElement[] stack = ex.getStackTrace();
if (stack != null && stack.length > 0) {
final StackTraceElement last = stack[0];
msg.append(last.getFileName()).append(':')
.append(last.getLineNumber()).append(':')
.append(' ');
}
msg.append(ex.getClass().getSimpleName())
.append(": ")
.append(ex.getMessage());
throw new EOerror.ExError(
new Data.ToPhi(msg.toString()),
this.label(suffix)
);
}
}

Expand Down
6 changes: 5 additions & 1 deletion eo-runtime/src/test/java/org/eolang/PhSafeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ public Phi take(final String name) {
"throws correct class"
).enclosure()
).take(String.class),
Matchers.equalTo("intentional error")
Matchers.allOf(
Matchers.startsWith("PhSafeTest.java:"),
Matchers.containsString("IllegalArgumentException"),
Matchers.containsString("intentional error")
)
);
}

Expand Down

0 comments on commit f9d7a3c

Please sign in to comment.