class ErrorMessageUtil {

  public static String buildErrorMessage(final Throwable throwable) {
    if (throwable == null) {
      return "";
    }

    final List<String> messages = getErrorMessages(throwable);
    dedup(messages);

    final String msg = messages.remove(0);

    final String causeMsg = messages.stream()
        .map(cause -> PREFIX + cause)
        .collect(Collectors.joining(System.lineSeparator()));

    return causeMsg.isEmpty() ? msg : msg + System.lineSeparator() + causeMsg;
  }

}