Class ExceptionUtil

java.lang.Object
jp.ecuacion.lib.core.util.ExceptionUtil

public class ExceptionUtil extends Object
Provides available utilities for Exceptions including AppExceptions.
  • Field Details

  • Method Details

    • getExceptionMessage

      @Nonnull public static List<String> getExceptionMessage(@RequireNonnull Throwable throwable, boolean needsDetails)
      Returns exception message for 1 exception.

      This method covers all the exceptions including Java standard exceptions, ConstraintViolationException used in Jakarta Validation and AppExceptions defined in this library.

      One exception normally has one message, but one ConstraintViolationException can have multiple messages so the return type is not a String, but a List<String>.

      Parameters:
      throwable - throwable
      needsDetails - Sets if detail message is needed. This is true with log output or batch processing. False when you show the message on screen.
      Returns:
      a list of messages
    • getExceptionMessage

      @Nonnull public static List<String> getExceptionMessage(@RequireNonnull Throwable throwable, @Nullable Locale locale, boolean needsDetails)
      Returns exception message for 1 exception.

      This method covers all the exceptions including Java standard exceptions, ConstraintViolationException used in Jakarta Validation and AppExceptions defined in this library.

      One exception normally has one message, but one ConstraintViolationException can have multiple messages so the return type is not a String, but a List<String>.

      Parameters:
      throwable - throwable
      locale - locale, may be null which is treated as Locale.getDefault().
      needsDetails - Sets if detail message is needed. This is true with log output or batch processing. False when you show the message on screen.
      Returns:
      a list of messages
    • getExceptionMessage

      @Nonnull public static List<String> getExceptionMessage(@RequireNonnull Throwable throwable, @Nullable Locale locale, boolean needsDetails, boolean needsItemName)
      Returns exception message for 1 exception.

      This method covers all the exceptions including Java standard exceptions, ConstraintViolationException used in Jakarta Validation and AppExceptions defined in this library.

      One exception normally has one message, but one ConstraintViolationException can have multiple messages so the return type is not a String, but a List<String>.

      Parameters:
      throwable - throwable
      locale - locale, may be null which is treated as Locale.getDefault().
      needsDetails - Sets if detail message is needed. This is true with log output or batch processing. False when you show the message on screen.
      needsItemName - true when itemName needed for ValidationAppException messages.
      Returns:
      a list of messages
    • getExceptionListWithMessages

      @Nonnull public static List<Throwable> getExceptionListWithMessages(@RequireNonnull Throwable throwable)
      Returns listed exceptions with messages.

      This is used for log output to see error messages easily.

      This is not used for online because in online apps messages shown to users is only SingleAppExceptions.

      Parameters:
      throwable - throwable
      Returns:
      a list of Throwables
    • getSingleAppExceptionList

      @Nonnull public static List<SingleAppException> getSingleAppExceptionList(@RequireNonnull AppException appException)
      Returns listed SingleAppExceptions (= AppExceptions with messages).

      This is not used for online

      Parameters:
      appException - AppException
      Returns:
      list of SingleAppException
    • getAppExceptionMessageList

      @Nonnull public static List<String> getAppExceptionMessageList(@RequireNonnull AppException appException, @Nullable Locale locale)
      Returns listed SingleAppExceptions (= AppExceptions with messages).

      This is used for online.

      The overload method with RuntimeAppException won't be created because getCause() of RuntimeAppException is AppException and it is not needed by then.

      Parameters:
      appException - AppException
      locale - locale, may be null which is treated as Locale.getDefault().
      Returns:
      list of SingleAppException
    • getAppExceptionMessageList

      @Nonnull public static List<String> getAppExceptionMessageList(@RequireNonnull AppException appException, @Nullable Locale locale, boolean needsItemName)
      Returns listed SingleAppExceptions (= AppExceptions with messages).

      This is used for online.

      The overload method with RuntimeAppException won't be created because getCause() of RuntimeAppException is AppException and it is not needed by then.

      Parameters:
      appException - AppException
      locale - locale, may be null which is treated as Locale.getDefault().
      needsItemName - true when itemName needed for ValidationAppException messages.
      Returns:
      list of SingleAppException
    • getErrLogString

      @Nonnull public static String getErrLogString(@RequireNonnull Throwable throwable, @Nullable String additionalMessage, @Nullable Locale locale)
      Returns strings or error log.
      Parameters:
      throwable - throwable
      additionalMessage - additional message, may be null if no additionalMessage is needed. In the case of null no additional message is output.
      locale - locale, may be null which is treated as Locale.getDefault().
      Returns:
      error log string
    • getErrLogString

      @Nonnull public static String getErrLogString(@RequireNonnull Throwable throwable, @Nullable String additionalMessage, @Nullable Locale locale, @Nullable Integer packagesShown)
      Returns strings or error log.
      Parameters:
      throwable - throwable
      additionalMessage - additional message, may be null if no additionalMessage is needed. In the case of null no additional message is output.
      locale - locale, may be null which is treated as Locale.getDefault().
      packagesShown - packages shown in the stack traces. This is used when the log displaying area is small.
      Returns:
      error log string
    • getMessageAndStackTraceStringRecursively

      public static void getMessageAndStackTraceStringRecursively(StringBuilder sb, Throwable th, Locale locale, Integer packagesShown)
      Adds Throwable message and stackTrace string to argument stringBuilder for a throwable and its causes.
      Parameters:
      sb - StringBuilder
      th - throwable
      locale - locale, may be null
      packagesShown - null means all package of a class is shown like "at jp.ecuacion.lib.core.util.ExceptionUtil.main(ExceptionUtil.java:468)". "0" shows no packages like "at ..main(ExceptionUtil.java:468)". "1" shows 1 package part like "at jp...main(ExceptionUtil.java:468)".