Package com.dianxin.tori.api.utils
Class ExceptionUtils
java.lang.Object
com.dianxin.tori.api.utils.ExceptionUtils
Utility class for sanitizing and formatting exception stack traces.
This class implements a thread-safe Singleton pattern and allows dynamic registration of package prefixes that should be filtered out (suppressed) from stack traces. This is particularly useful for keeping server logs clean from deeply nested internal framework calls (e.g., Reactor, Netty, or Jsoup).
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull ExceptionUtilsRetrieves the singleton instance ofExceptionUtils.booleanisSuppressedPackage(@NotNull String className) Checks if the specified class name belongs to any currently registered suppressed package.booleanregisterSupressedPackage(@NotNull String packageName) Dynamically registers a package prefix to be suppressed from future stack traces.Sanitizes the givenThrowableby intercepting its stack trace and removing any elements that belong to registered suppressed packages.booleanunregisterSupressedPackage(@NotNull String packageName) Unregisters a previously suppressed package prefix.
-
Method Details
-
getInstance
Retrieves the singleton instance ofExceptionUtils.Utilizes double-checked locking to ensure thread safety without synchronization overhead on every call.
- Returns:
- The globally available instance of
ExceptionUtils.
-
isSuppressedPackage
Checks if the specified class name belongs to any currently registered suppressed package.- Parameters:
className- The full class name to check (e.g., "org.jsoup.Connection").- Returns:
trueif the class belongs to a suppressed package,falseotherwise.
-
registerSupressedPackage
Dynamically registers a package prefix to be suppressed from future stack traces.Bots or plugins can use this method to hide their internal noisy dependencies from the main server logs.
- Parameters:
packageName- The package prefix to suppress (e.g., "org.jsoup").- Returns:
trueif the package was successfully added,falseif it was already registered.
-
unregisterSupressedPackage
Unregisters a previously suppressed package prefix.- Parameters:
packageName- The package prefix to remove from the suppression list.- Returns:
trueif the package was successfully removed,falseif it was not found.
-
sanitize
Sanitizes the givenThrowableby intercepting its stack trace and removing any elements that belong to registered suppressed packages.This method operates recursively to ensure that all nested errors (Caused by) are also thoroughly sanitized.
- Parameters:
throwable- The exception to sanitize. Can be null.- Returns:
- The sanitized exception, or
nullif the input was null.
-