Skip to content

Commit

Permalink
Merge pull request #3669 from objectionary/3668
Browse files Browse the repository at this point in the history
prints logs with all traces
  • Loading branch information
yegor256 authored Dec 15, 2024
2 parents d0e4cbd + 49fb681 commit 15c4701
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 21 deletions.
32 changes: 16 additions & 16 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOerror.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ public static final class ExError extends ExAbstract {
private final Phi enc;

/**
* Locations seen on its way out.
* Messages seen on its way out.
*/
private final Collection<String> locs;
private final Collection<String> trace;

/**
* Ctor.
Expand All @@ -104,21 +104,21 @@ public ExError(final Phi enclosure) {
/**
* Ctor.
* @param cause Previous error
* @param loc New location
* @param message New message
*/
public ExError(final ExError cause, final String loc) {
this(cause.enclosure(), concat(cause.locs, loc));
public ExError(final ExError cause, final String message) {
this(cause.enclosure(), concat(cause.trace, message));
}

/**
* Ctor.
* @param enclosure Enclosure inside the error
* @param locations Locations seen
* @param before Messages seen before
*/
public ExError(final Phi enclosure, final Collection<String> locations) {
public ExError(final Phi enclosure, final Collection<String> before) {
super(EOerror.ExError.safeMessage(enclosure));
this.enc = enclosure;
this.locs = locations;
this.trace = before;
}

/**
Expand All @@ -130,24 +130,24 @@ public Phi enclosure() {
}

/**
* Take locations.
* @return The locations
* Take earlier seen messages.
* @return The messages
*/
public Collection<String> locations() {
return this.locs;
public Collection<String> messages() {
return this.trace;
}

/**
* Concatenate locations.
* Concatenate messages.
* @param before Locations before
* @param loc New one
* @param message New one
* @return New list of them
*/
private static Collection<String> concat(final Collection<String> before,
final String loc) {
final String message) {
final Collection<String> list = new ArrayList<>(before.size() + 1);
list.addAll(before);
list.add(loc);
list.add(message);
return list;
}

Expand Down
4 changes: 2 additions & 2 deletions eo-runtime/src/main/java/org/eolang/Dataized.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ public byte[] take() {
}
return data;
} catch (final EOerror.ExError ex) {
final List<String> raw = new ArrayList<>(ex.locations().size());
raw.addAll(ex.locations());
final List<String> raw = new ArrayList<>(ex.messages().size());
raw.addAll(ex.messages());
Collections.reverse(raw);
if ("org.eolang.string".equals(ex.enclosure().forma())) {
raw.add(
Expand Down
25 changes: 24 additions & 1 deletion eo-runtime/src/main/java/org/eolang/PhSafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
package org.eolang;

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

/**
* It catches {@link ExFailure} and
Expand Down Expand Up @@ -114,8 +117,13 @@ private static <T> T through(final Action<T> action) {
try {
return action.act();
} catch (final ExFailure ex) {
final List<String> before = PhSafe.messages(ex);
if (!before.isEmpty()) {
before.remove(0);
}
throw new EOerror.ExError(
new Data.ToPhi(PhSafe.message(ex))
new Data.ToPhi(PhSafe.message(ex)),
before
);
}
}
Expand Down Expand Up @@ -143,4 +151,19 @@ private static String message(final Throwable exp) {
return ret.toString();
}

/**
* Make a chain of messages from an exception and its causes.
* @param exp The exception
* @return Messages
*/
private static List<String> messages(final Throwable exp) {
final List<String> msgs = new LinkedList<>();
if (exp != null) {
msgs.add(exp.getMessage());
msgs.addAll(PhSafe.messages(exp.getCause()));
Collections.reverse(msgs);
}
return msgs;
}

}
39 changes: 37 additions & 2 deletions eo-runtime/src/test/java/org/eolang/DataizedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

/**
* Test case for {@link Dataized}.
*
* @since 0.22
*/
@Execution(ExecutionMode.SAME_THREAD)
final class DataizedTest {
/**
* System property for maximum dataization log level.
Expand Down Expand Up @@ -76,11 +79,10 @@ void logsWhenException() {
final List<LogRecord> logs = new LinkedList<>();
final Handler hnd = new Hnd(logs);
log.addHandler(hnd);
final Phi wrong = new PhIncorrect();
IntStream.range(0, 5).forEach(
i -> Assertions.assertThrows(
ExFailure.class,
() -> new Dataized(wrong).take(),
() -> new Dataized(new PhIncorrect()).take(),
"Expected failure with ExFailure exception on incorrect object dataization"
)
);
Expand All @@ -97,6 +99,39 @@ void logsWhenException() {
);
}

@Test
void logsAllLocationsWithPhSafe() {
final Logger log = Logger.getLogger("logsWithPhSafe");
final Level before = log.getLevel();
log.setLevel(Level.ALL);
final List<LogRecord> logs = new LinkedList<>();
final Handler hnd = new Hnd(logs);
log.addHandler(hnd);
Assertions.assertThrows(
EOerror.ExError.class,
() -> new Dataized(
new PhSafe(
new PhLocated(new PhIncorrect(), "foo.bar", 0, 0)
),
log
).take(),
"it is expected to fail with ExFailure exception"
);
log.setLevel(before);
log.removeHandler(hnd);
MatcherAssert.assertThat(
"all messages should be logged",
logs.get(0).getMessage(),
Matchers.allOf(
Matchers.containsString("1) Error in"),
Matchers.containsString("2) \"There's"),
Matchers.not(Matchers.containsString("3)")),
Matchers.containsString("at foo.bar:0:0"),
Matchers.containsString("no data in the object, can't take it")
)
);
}

@Test
void failsWhenError() {
Assertions.assertThrows(
Expand Down

0 comments on commit 15c4701

Please sign in to comment.