Class ExceptionsHelper

java.lang.Object
org.elasticsearch.ExceptionsHelper

public final class ExceptionsHelper extends Object
  • Constructor Details

    • ExceptionsHelper

      public ExceptionsHelper()
  • Method Details

    • convertToRuntime

      public static RuntimeException convertToRuntime(Exception e)
    • convertToElastic

      public static ElasticsearchException convertToElastic(Exception e)
    • status

      public static RestStatus status(Throwable t)
    • unwrapCause

      public static Throwable unwrapCause(Throwable t)
    • stackTrace

      public static String stackTrace(Throwable e)
    • limitedStackTrace

      public static String limitedStackTrace(Throwable e, int traceDepth)
      Constructs a limited and compressed stack trace string. Each exception printed as part of the full stack trace will have its printed stack frames capped at the given trace depth. Stack traces that are longer than the given trace depth will summarize the count of the remaining frames at the end of the trace. Each stack frame omits the module information and limits the package names to single characters per part.

      An example result when using a trace depth of 2 and one nested cause:
      
       o.e.s.GenericException: some generic exception!
         at o.e.s.SomeClass.method(SomeClass.java:100)
         at o.e.s.SomeOtherClass.earlierMethod(SomeOtherClass.java:24)
         ... 5 more
       Caused by: o.e.s.GenericException: some other generic exception!
         at o.e.s.SomeClass.method(SomeClass.java:115)
         at o.e.s.SomeOtherClass.earlierMethod(SomeOtherClass.java:16)
         ... 12 more
       
      Parameters:
      e - Throwable object to construct a printed stack trace for
      traceDepth - The maximum number of stack trace elements to display per exception referenced
      Returns:
      A string containing a limited and compressed stack trace.
    • formatStackTrace

      public static String formatStackTrace(StackTraceElement[] stackTrace)
    • rethrowAndSuppress

      public static <T extends Throwable> void rethrowAndSuppress(List<T> exceptions) throws T
      Rethrows the first exception in the list and adds all remaining to the suppressed list. If the given list is empty no exception is thrown
      Throws:
      T
    • maybeThrowRuntimeAndSuppress

      public static <T extends Throwable> void maybeThrowRuntimeAndSuppress(List<T> exceptions)
      Throws a runtime exception with all given exceptions added as suppressed. If the given list is empty no exception is thrown
    • useOrSuppress

      public static <T extends Throwable> T useOrSuppress(T first, T second)
    • unwrapCorruption

      public static IOException unwrapCorruption(Throwable t)
      Looks at the given Throwable's and its cause(s) as well as any suppressed exceptions on the Throwable as well as its causes and returns the first corruption indicating exception (as defined by CORRUPTION_EXCEPTIONS) it finds.
      Parameters:
      t - Throwable
      Returns:
      Corruption indicating exception if one is found, otherwise null
    • unwrap

      public static Throwable unwrap(Throwable t, Class<?>... clazzes)
      Looks at the given Throwable and its cause(s) and returns the first Throwable that is of one of the given classes or null if no matching Throwable is found. Unlike unwrapCorruption(java.lang.Throwable) this method does only check the given Throwable and its causes but does not look at any suppressed exceptions.
      Parameters:
      t - Throwable
      clazzes - Classes to look for
      Returns:
      Matching Throwable if one is found, otherwise null
    • reThrowIfNotNull

      public static boolean reThrowIfNotNull(@Nullable Throwable e)
      Throws the specified exception. If null if specified then true is returned.
    • unwrapCausesAndSuppressed

      public static <T extends Throwable> Optional<T> unwrapCausesAndSuppressed(Throwable cause, Predicate<Throwable> predicate)
    • maybeError

      public static Optional<Error> maybeError(Throwable cause)
      Unwrap the specified throwable looking for any suppressed errors or errors as a root cause of the specified throwable.
      Parameters:
      cause - the root throwable
      Returns:
      an optional error if one is found suppressed or a root cause in the tree rooted at the specified throwable
    • maybeDieOnAnotherThread

      public static void maybeDieOnAnotherThread(Throwable throwable)
      If the specified cause is an unrecoverable error, this method will rethrow the cause on a separate thread so that it can not be caught and bubbles up to the uncaught exception handler. Note that the cause tree is examined for any Error. See maybeError(Throwable) for the semantics.
      Parameters:
      throwable - the throwable to possibly throw on another thread
    • groupBy

      public static ShardOperationFailedException[] groupBy(ShardOperationFailedException[] failures)
      Deduplicate the failures by exception message and index.
      Parameters:
      failures - array to deduplicate
      Returns:
      deduplicated array; if failures is null or empty, it will be returned without modification
    • isNodeOrShardUnavailableTypeException

      public static boolean isNodeOrShardUnavailableTypeException(Throwable t)
      Utility method useful for determining whether to log an Exception or perhaps avoid logging a stacktrace if the caller/logger is not interested in these types of node/shard issues.
      Parameters:
      t - Throwable to inspect
      Returns:
      true if the Throwable is an instance of an Exception that indicates that either a Node or shard is unavailable/disconnected.
    • isRemoteUnavailableException

      public static boolean isRemoteUnavailableException(Exception e)
      Checks the exception against a known list of exceptions that indicate a remote cluster cannot be connected to.
      Parameters:
      e - Exception to inspect
      Returns:
      true if the Exception is known to indicate that a remote cluster is unavailable (cannot be connected to by the transport layer)
    • isTaskCancelledException

      public static boolean isTaskCancelledException(Exception e)
      Utility method to check if an Exception is/was caused by TaskCancelledException.
      Parameters:
      e - Exception we're interested in evaluating.
      Returns:
      true if the Exception is/was caused by TaskCancelledException, else false.