Class ExceptionMatcher

java.lang.Object
org.springframework.kafka.support.ExceptionMatcher

public class ExceptionMatcher extends Object
Configurable exception matcher with support for identifying matching exception in nested causes. The matcher can be configured in two ways:
  1. With an allow list, only the registered exception matches. This includes exceptions that extend from any registered exception or implement it if that's an interface.
  2. With a () deny list, an exception match only if it isn't found amongst the registered exceptions. As for the allowlist, any exception that extends or implements one of the registered exceptions will lead it to not match.
By default, only the given Throwable is inspected. To also search nested causes, traverseCauses should be enabled.
Since:
4.0
Author:
Stephane Nicoll, Dave Syer, Gary Russell
  • Constructor Details

    • ExceptionMatcher

      public ExceptionMatcher(Collection<Class<? extends Throwable>> exceptionTypes, boolean shouldMatchIfFound)
      Create an instance with a list of exceptions. The shouldMatchIfFound parameter determines what should happen if an exception is found within that list.
      Parameters:
      exceptionTypes - the exceptions to register
      shouldMatchIfFound - match result if a candidate exception is found amongst the given list
    • ExceptionMatcher

      protected ExceptionMatcher(Map<Class<? extends Throwable>, Boolean> entries, boolean matchIfNotFound, boolean traverseCauses)
  • Method Details

    • defaultMatcher

      public static ExceptionMatcher defaultMatcher()
      Create a matcher that matches any Exception, but not errors.
      Returns:
      a matcher that matches any exception, but not errors
    • forAllowList

      public static ExceptionMatcher.Builder forAllowList()
      Create a builder for a matcher that only matches an exception that is found in the configurable list of exception types.
      Returns:
      a ExceptionMatcher.Builder that configures an allowlist of exceptions
    • forDenyList

      public static ExceptionMatcher.Builder forDenyList()
      Create a builder for a matcher that only matches an exception that is not found in the configurable list of exception types.
      Returns:
      a ExceptionMatcher.Builder that configures a denylist of exceptions
    • setTraverseCauses

      public void setTraverseCauses(boolean traverseCauses)
      Specify if this match should traverse nested causes to check for the presence of a matching exception.
      Parameters:
      traverseCauses - whether to traverse causes
    • match

      public boolean match(@Nullable Throwable exception)
      Specify if the given Throwable match this instance.
      Parameters:
      exception - the exception to check
      Returns:
      true if this exception match this instance, false otherwise
    • getEntries

      protected Map<Class<? extends Throwable>, Boolean> getEntries()