Class Exceptions

java.lang.Object
com.linecorp.armeria.common.util.Exceptions

public final class Exceptions extends Object
Provides methods that are useful for handling exceptions.
  • Method Details

    • logIfUnexpected

      public static void logIfUnexpected(Logger logger, Channel ch, Throwable cause)
      Logs the specified exception if it is unexpected.
    • logIfUnexpected

      public static void logIfUnexpected(Logger logger, Channel ch, String debugData, Throwable cause)
      Logs the specified exception if it is unexpected.
    • logIfUnexpected

      public static void logIfUnexpected(Logger logger, Channel ch, @Nullable @Nullable SessionProtocol protocol, Throwable cause)
      Logs the specified exception if it is unexpected.
    • logIfUnexpected

      public static void logIfUnexpected(Logger logger, Channel ch, @Nullable @Nullable SessionProtocol protocol, String debugData, Throwable cause)
      Logs the specified exception if it is unexpected.
    • isExpected

      public static boolean isExpected(Throwable cause)
      Returns true if the specified exception is expected to occur in well-known circumstances.
      See Also:
    • isStreamCancelling

      public static boolean isStreamCancelling(Throwable cause)
      Returns true if the specified exception will cancel the current request or response stream.
    • clearTrace

      public static <T extends Throwable> T clearTrace(T exception)
      Empties the stack trace of the specified exception.
    • throwUnsafely

      public static <T> T throwUnsafely(Throwable cause)
      Throws the specified exception violating the throws clause of the enclosing method. This method is useful when you need to rethrow a checked exception in Function, Consumer, Supplier and Runnable, only if you are sure that the rethrown exception will be handled as a Throwable or an Exception. For example:
      
       CompletableFuture.supplyAsync(() -> {
           try (FileInputStream fin = new FileInputStream(...)) {
               ....
               return someValue;
           } catch (IOException e) {
               // 'throw e;' won't work because Runnable.run() does not allow any checked exceptions.
               return Exceptions.throwUnsafely(e);
           }
       }).exceptionally(CompletionActions::log);
       
      Returns:
      This method never returns because it always throws an exception. However, combined with an arbitrary return clause, you can terminate any non-void function with a single statement. e.g. return Exceptions.throwUnsafely(...); vs. Exceptions.throwUnsafely(...); return null;
    • throwIfFatal

      public static void throwIfFatal(Throwable t)
      Throws a particular Throwable only if it belongs to a set of "fatal" error varieties. These varieties are as follows:
      • VirtualMachineError
      • ThreadDeath
      • LinkageError
      This can be useful if you are writing an operator that calls user-supplied code, and you want to notify subscribers of errors encountered in that code by calling their onError methods, but only if the errors are not so catastrophic that such a call would be futile, in which case you simply want to rethrow the error.
      Parameters:
      t - the Throwable to test and perhaps throw
      See Also:
    • peel

      public static Throwable peel(Throwable throwable)
      Returns the cause of the specified throwable peeling it recursively, if it is one of the CompletionException, ExecutionException, InvocationTargetException or ExceptionInInitializerError. Otherwise returns the throwable.
    • traceText

      public static String traceText(Throwable exception)
      Converts the stack trace of the specified exception into a String. This method always uses '\n' as a line delimiter, unlike Throwable.printStackTrace(PrintWriter) or Throwables.getStackTraceAsString(Throwable).