Skip to content

Commit fd698bc

Browse files
committed
Improve error ignored check
1 parent 5405b3a commit fd698bc

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

core/src/main/java/dev/faststats/core/SimpleErrorTracker.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.jspecify.annotations.Nullable;
66

77
import java.lang.Thread.UncaughtExceptionHandler;
8+
import java.util.Collections;
9+
import java.util.IdentityHashMap;
810
import java.util.Map;
911
import java.util.Optional;
1012
import java.util.Set;
@@ -43,14 +45,7 @@ public void trackError(final String message, final boolean handled) {
4345
@Override
4446
public void trackError(final Throwable error, final boolean handled) {
4547
try {
46-
if (ignoredTypes.contains(error.getClass())) return;
47-
48-
final var message = error.getMessage() != null ? error.getMessage() : "";
49-
if (ignoredPatterns.stream().map(pattern -> pattern.matcher(message)).anyMatch(Matcher::find)) return;
50-
51-
final var typedPatterns = ignoredTypedPatterns.get(error.getClass());
52-
if (typedPatterns != null && typedPatterns.stream().map(pattern -> pattern.matcher(message)).anyMatch(Matcher::find)) return;
53-
48+
if (isIgnored(error, Collections.newSetFromMap(new IdentityHashMap<>()))) return;
5449
final var compiled = ErrorHelper.compile(error, null, handled);
5550
final var hashed = MurmurHash3.hash(compiled);
5651
if (collected.compute(hashed, (k, v) -> {
@@ -61,6 +56,21 @@ public void trackError(final Throwable error, final boolean handled) {
6156
}
6257
}
6358

59+
private boolean isIgnored(@Nullable final Throwable error, final Set<Throwable> visited) {
60+
if (error == null || !visited.add(error)) return false;
61+
62+
if (ignoredTypes.contains(error.getClass())) return true;
63+
64+
final var message = error.getMessage() != null ? error.getMessage() : "";
65+
if (ignoredPatterns.stream().map(pattern -> pattern.matcher(message)).anyMatch(Matcher::find)) return true;
66+
67+
final var patterns = ignoredTypedPatterns.get(error.getClass());
68+
if (patterns != null && patterns.stream().map(pattern -> pattern.matcher(message)).anyMatch(Matcher::find))
69+
return true;
70+
71+
return isIgnored(error.getCause(), visited);
72+
}
73+
6474
@Override
6575
public ErrorTracker ignoreErrorType(final Class<? extends Throwable> type) {
6676
ignoredTypes.add(type);

0 commit comments

Comments
 (0)