|
1 | 1 | package dev.faststats.core; |
2 | 2 |
|
| 3 | +import org.intellij.lang.annotations.RegExp; |
3 | 4 | import org.jetbrains.annotations.Contract; |
4 | 5 | import org.jspecify.annotations.Nullable; |
5 | 6 |
|
6 | 7 | import java.util.Optional; |
7 | 8 | import java.util.function.BiConsumer; |
| 9 | +import java.util.regex.Pattern; |
8 | 10 |
|
9 | 11 | /** |
10 | 12 | * An error tracker. |
@@ -96,6 +98,85 @@ static ErrorTracker contextUnaware() { |
96 | 98 | @Contract(mutates = "this") |
97 | 99 | void trackError(Throwable error, boolean handled); |
98 | 100 |
|
| 101 | + /** |
| 102 | + * Adds an error type that will not be reported to FastStats. |
| 103 | + * <p> |
| 104 | + * Matching is done exactly. If, for example {@link LinkageError} was ignored, |
| 105 | + * {@link NoClassDefFoundError} would still be reported, even though it extends {@link LinkageError} |
| 106 | + * |
| 107 | + * @param type the error type |
| 108 | + * @return the error tracker |
| 109 | + * @since 0.21.0 |
| 110 | + */ |
| 111 | + @Contract(value = "_ -> this", mutates = "this") |
| 112 | + ErrorTracker ignoreErrorType(Class<? extends Throwable> type); |
| 113 | + |
| 114 | + /** |
| 115 | + * Adds a pattern that will be matched against all error messages. |
| 116 | + * <p> |
| 117 | + * If an error's message matches the given pattern, it will not be reported to FastStats. |
| 118 | + * <pre>{@code |
| 119 | + * // Exact match |
| 120 | + * tracker.ignoreError(Pattern.compile("No space left on device")); |
| 121 | + * |
| 122 | + * // Regex match |
| 123 | + * tracker.ignoreError(Pattern.compile("No serializer for: class .*")); |
| 124 | + * }</pre> |
| 125 | + * |
| 126 | + * @param pattern the regex pattern to match against error messages |
| 127 | + * @return the error tracker |
| 128 | + * @since 0.21.0 |
| 129 | + */ |
| 130 | + @Contract(value = "_ -> this", mutates = "this") |
| 131 | + ErrorTracker ignoreError(Pattern pattern); |
| 132 | + |
| 133 | + /** |
| 134 | + * Adds a pattern that will be matched against all error messages. |
| 135 | + * <p> |
| 136 | + * If an error's message matches the given pattern, it will not be reported to FastStats. |
| 137 | + * |
| 138 | + * @param pattern the regex pattern string to match against error messages |
| 139 | + * @return the error tracker |
| 140 | + * @see #ignoreError(Pattern) |
| 141 | + * @since 0.21.0 |
| 142 | + */ |
| 143 | + @Contract(value = "_ -> this", mutates = "this") |
| 144 | + default ErrorTracker ignoreError(@RegExp final String pattern) { |
| 145 | + return ignoreError(Pattern.compile(pattern)); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Adds an error type combined with a message pattern that will not be reported to FastStats. |
| 150 | + * <p> |
| 151 | + * An error is ignored only if its class matches the given type exactly and its message matches the given pattern. |
| 152 | + * <pre>{@code |
| 153 | + * tracker.ignoreError(IOException.class, Pattern.compile("No space left on device")); |
| 154 | + * }</pre> |
| 155 | + * |
| 156 | + * @param type the error type |
| 157 | + * @param pattern the regex pattern to match against error messages |
| 158 | + * @return the error tracker |
| 159 | + * @since 0.21.0 |
| 160 | + */ |
| 161 | + @Contract(value = "_, _ -> this", mutates = "this") |
| 162 | + ErrorTracker ignoreError(Class<? extends Throwable> type, Pattern pattern); |
| 163 | + |
| 164 | + /** |
| 165 | + * Adds an error type combined with a message pattern that will not be reported to FastStats. |
| 166 | + * <p> |
| 167 | + * An error is ignored only if its class matches the given type exactly and its message matches the given pattern. |
| 168 | + * |
| 169 | + * @param type the error type |
| 170 | + * @param pattern the regex pattern string to match against error messages |
| 171 | + * @return the error tracker |
| 172 | + * @see #ignoreError(Class, Pattern) |
| 173 | + * @since 0.21.0 |
| 174 | + */ |
| 175 | + @Contract(value = "_, _ -> this", mutates = "this") |
| 176 | + default ErrorTracker ignoreError(final Class<? extends Throwable> type, @RegExp final String pattern) { |
| 177 | + return ignoreError(type, Pattern.compile(pattern)); |
| 178 | + } |
| 179 | + |
99 | 180 | /** |
100 | 181 | * Attaches an error context to the tracker. |
101 | 182 | * <p> |
|
0 commit comments