/**
  * {@link RemoteUtil#unwrap(Throwable) unwraps} given exception if possible and builds error
  * message for it.
  *
  * @param e exception to process
  * @return error message for the given exception
  */
 @SuppressWarnings({"ThrowableResultOfMethodCallIgnored", "IOResourceOpenedButNotSafelyClosed"})
 @NotNull
 public static String buildErrorMessage(@NotNull Throwable e) {
   Throwable unwrapped = RemoteUtil.unwrap(e);
   String reason = unwrapped.getLocalizedMessage();
   if (!StringUtil.isEmpty(reason)) {
     return reason;
   } else if (unwrapped.getClass() == ExternalSystemException.class) {
     return String.format(
         "exception during working with external system: %s",
         ((ExternalSystemException) unwrapped).getOriginalReason());
   } else {
     StringWriter writer = new StringWriter();
     unwrapped.printStackTrace(new PrintWriter(writer));
     return writer.toString();
   }
 }