Class ExceptionUtils

java.lang.Object
com.dianxin.tori.api.utils.ExceptionUtils

public class ExceptionUtils extends Object
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 Details

    • getInstance

      @NotNull public static @NotNull ExceptionUtils getInstance()
      Retrieves the singleton instance of ExceptionUtils.

      Utilizes double-checked locking to ensure thread safety without synchronization overhead on every call.

      Returns:
      The globally available instance of ExceptionUtils.
    • isSuppressedPackage

      public boolean isSuppressedPackage(@NotNull @NotNull String className)
      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:
      true if the class belongs to a suppressed package, false otherwise.
    • registerSupressedPackage

      public boolean registerSupressedPackage(@NotNull @NotNull String packageName)
      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:
      true if the package was successfully added, false if it was already registered.
    • unregisterSupressedPackage

      public boolean unregisterSupressedPackage(@NotNull @NotNull String packageName)
      Unregisters a previously suppressed package prefix.
      Parameters:
      packageName - The package prefix to remove from the suppression list.
      Returns:
      true if the package was successfully removed, false if it was not found.
    • sanitize

      public Throwable sanitize(@UnknownNullability Throwable throwable)
      Sanitizes the given Throwable by 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 null if the input was null.