Skip to content

Commit 5847aa6

Browse files
authored
Add handled error tracking to ErrorTracker (#123)
Introduced `handled` parameter to `trackError` methods for more accurate error categorization. Updated `compile` method in `ErrorHelper` to include the `handled` field and adjusted related implementations.
1 parent 8efa19a commit 5847aa6

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

bukkit/src/main/java/dev/faststats/bukkit/PaperEventListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public void onServerException(final ServerExceptionEvent event) {
1212
if (!(event.getException() instanceof final ServerPluginException exception)) return;
1313
if (!exception.getResponsiblePlugin().equals(metrics.plugin())) return;
1414
final var report = exception.getCause() != null ? exception.getCause() : exception;
15-
metrics.getErrorTracker().ifPresent(tracker -> tracker.trackError(report));
15+
metrics.getErrorTracker().ifPresent(tracker -> tracker.trackError(report, false));
1616
}
1717
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ final class ErrorHelper {
1717
private static final int STACK_TRACE_LENGTH = Math.min(500, Integer.getInteger("faststats.stack-trace-length", 300));
1818
private static final int STACK_TRACE_LIMIT = Math.min(50, Integer.getInteger("faststats.stack-trace-limit", 15));
1919

20-
public static JsonObject compile(final Throwable error, @Nullable final List<String> suppress) {
20+
public static JsonObject compile(final Throwable error, @Nullable final List<String> suppress, final boolean handled) {
2121
final var report = new JsonObject();
2222
final var message = getAnonymizedMessage(error);
2323

@@ -40,6 +40,7 @@ public static JsonObject compile(final Throwable error, @Nullable final List<Str
4040
if (message != null) report.addProperty("message", message);
4141

4242
report.add("stack", stacktrace);
43+
report.addProperty("handled", true);
4344

4445
return report;
4546
}

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

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public sealed interface ErrorTracker permits SimpleErrorTracker {
2121
*
2222
* @return the error tracker
2323
* @see #contextUnaware()
24-
* @see #trackError(String)
25-
* @see #trackError(Throwable)
24+
* @see #trackError(String, boolean)
25+
* @see #trackError(Throwable, boolean)
2626
* @since 0.10.0
2727
*/
2828
@Contract(value = " -> new")
@@ -51,24 +51,51 @@ static ErrorTracker contextUnaware() {
5151
}
5252

5353
/**
54-
* Tracks an error.
54+
* Tracks a handled error.
5555
*
5656
* @param message the error message
5757
* @see #trackError(Throwable)
58+
* @see #trackError(String, boolean)
5859
* @since 0.10.0
5960
*/
6061
@Contract(mutates = "this")
6162
void trackError(String message);
6263

6364
/**
64-
* Tracks an error.
65+
* Tracks a handled error.
6566
*
6667
* @param error the error
68+
* @see #trackError(Throwable, boolean)
6769
* @since 0.10.0
6870
*/
6971
@Contract(mutates = "this")
7072
void trackError(Throwable error);
7173

74+
/**
75+
* Tracks an error.
76+
* <p>
77+
* A {@code handled=true} error is expected and properly handled.
78+
*
79+
* @param message the error message
80+
* @param handled whether the error was handled
81+
* @see #trackError(Throwable, boolean)
82+
* @since 0.20.0
83+
*/
84+
@Contract(mutates = "this")
85+
void trackError(String message, boolean handled);
86+
87+
/**
88+
* Tracks an error.
89+
* <p>
90+
* A {@code handled=true} error is expected and properly handled.
91+
*
92+
* @param error the error
93+
* @param handled whether the error was handled
94+
* @since 0.20.0
95+
*/
96+
@Contract(mutates = "this")
97+
void trackError(Throwable error, boolean handled);
98+
7299
/**
73100
* Attaches an error context to the tracker.
74101
* <p>

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,23 @@ final class SimpleErrorTracker implements ErrorTracker {
1919

2020
@Override
2121
public void trackError(final String message) {
22-
trackError(new RuntimeException(message));
22+
trackError(message, true);
2323
}
2424

2525
@Override
2626
public void trackError(final Throwable error) {
27+
trackError(error, true);
28+
}
29+
30+
@Override
31+
public void trackError(final String message, final boolean handled) {
32+
trackError(new RuntimeException(message), handled);
33+
}
34+
35+
@Override
36+
public void trackError(final Throwable error, final boolean handled) {
2737
try {
28-
final var compiled = ErrorHelper.compile(error, null);
38+
final var compiled = ErrorHelper.compile(error, null, handled);
2939
final var hashed = MurmurHash3.hash(compiled);
3040
if (collected.compute(hashed, (k, v) -> {
3141
return v == null ? 1 : v + 1;
@@ -84,9 +94,9 @@ public synchronized void attachErrorContext(@Nullable final ClassLoader loader)
8494
if (loader != null && !ErrorTracker.isSameLoader(loader, error)) return;
8595
final var event = errorEvent;
8696
if (event != null) event.accept(loader, error);
87-
trackError(error);
97+
trackError(error, false);
8898
} catch (final Throwable t) {
89-
trackError(t);
99+
trackError(t, false);
90100
}
91101
});
92102
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version=0.19.0
1+
version=0.20.0

0 commit comments

Comments
 (0)