Skip to content

Commit

Permalink
Merge pull request #3748 from objectionary/3747
Browse files Browse the repository at this point in the history
better logging of errors
  • Loading branch information
yegor256 authored Dec 24, 2024
2 parents 78f13fd + f115e45 commit 9183912
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
57 changes: 43 additions & 14 deletions eo-runtime/src/main/java/org/eolang/PhSafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
package org.eolang;

import EOorg.EOeolang.EOerror;
import java.util.LinkedList;
import java.util.List;

/**
* An object with coordinates (line and position) and a safe
Expand Down Expand Up @@ -197,25 +199,52 @@ private <T> T through(final Action<T> action, final String suffix) {
this.label(suffix)
);
} catch (final Throwable 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(": ");
}
msg.append(ex.getClass().getSimpleName())
.append(": ")
.append(ex.getMessage());
throw new EOerror.ExError(
new Data.ToPhi(msg.toString()),
this.label(suffix)
new Data.ToPhi(ex.getMessage()),
PhSafe.trace(ex, this.label(suffix))
);
}
}

/**
* Take stacktrace from exception.
* @param exp The exception
* @param head The head to add
* @return The stacktrace
*/
private static List<String> trace(final Throwable exp, final String head) {
final StackTraceElement[] stack = exp.getStackTrace();
final List<String> trace = new LinkedList<>();
if (stack != null) {
for (final StackTraceElement elm : stack) {
trace.add(
String.format(
"%s#%s():%d",
PhSafe.shorter(elm.getClassName()),
elm.getMethodName(),
elm.getLineNumber()
)
);
}
}
trace.add(
String.format(
"%s (%s)",
head, PhSafe.shorter(exp.getClass().getName())
)
);
return trace;
}

/**
* Make class name shorter.
* @param full The full name of class
* @return Shorter name
*/
private static String shorter(final String full) {
return full.replaceAll("(.)[^.]*\\.", "$1.");
}

/**
* The label of the exception.
* @param suffix The suffix to add to the label
Expand Down
10 changes: 4 additions & 6 deletions eo-runtime/src/test/java/org/eolang/PhSafeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public byte[] delta() {
).delta(),
"throws correct class"
).messages(),
Matchers.hasItem(
Matchers.hasItems(
Matchers.containsString("o.e.PhSafe#delta()"),
Matchers.containsString("(j.l.IllegalArgumentException)"),
Matchers.containsString("Error in \"?.Δ\" at unknown:0:0")
)
);
Expand Down Expand Up @@ -107,11 +109,7 @@ public Phi take(final String name) {
"throws correct class"
).enclosure()
).take(String.class),
Matchers.allOf(
Matchers.startsWith("PhSafeTest.java:"),
Matchers.containsString("IllegalArgumentException"),
Matchers.containsString("intentional error")
)
Matchers.equalTo("intentional error")
);
}

Expand Down

0 comments on commit 9183912

Please sign in to comment.