All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.commercetools.sync.commons.exceptions.ExceptionUtils Maven / Gradle / Ivy

Go to download

Java Library used to import and/or sync (taking care of changes) data into one or more commercetools projects from external sources such as CSV, XML, JSON, etc.. or even from an already existing commercetools project.

The newest version!
package com.commercetools.sync.commons.exceptions;

import static java.lang.String.format;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.StringUtils;

final class ExceptionUtils {
  @Nonnull
  static String buildMessage(
      @Nonnull final String message,
      @Nonnull final Set causes,
      final int causesTabbingDepth) {
    final String causesString = asMessage(causes, causesTabbingDepth);
    return isNotBlank(causesString) ? format("%s %s", message, causesString) : message;
  }

  @Nonnull
  private static String asMessage(@Nonnull final Set causes, final int tabbingDepth) {
    final String tabs = buildTabs(tabbingDepth);
    final List causesMessages =
        causes.stream()
            .map(Throwable::getMessage)
            .filter(StringUtils::isNotBlank)
            .collect(Collectors.toList());
    return !causesMessages.isEmpty() ? buildCausesMessage(tabs, causesMessages) : "";
  }

  @Nonnull
  private static String buildCausesMessage(
      @Nonnull final String tabs, @Nonnull final List causesMessages) {
    return format("Causes:%n%s%s", tabs, joinCauses(tabs, causesMessages));
  }

  @Nonnull
  private static String joinCauses(
      @Nonnull final String tabs, @Nonnull final List causesMessages) {
    return String.join(format("%n%s", tabs), causesMessages);
  }

  @Nonnull
  private static String buildTabs(final int numberOfTabs) {
    return IntStream.range(0, numberOfTabs).mapToObj(index -> "\t").collect(Collectors.joining());
  }

  private ExceptionUtils() {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy