package io.bit3.jsass.type;

import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Helper class to convert libsass to java values and vise versa.
 */
public class TypeUtils {
  /**
   * Try to convert "any" java object into a responsible sass value.
   */
  public static SassValue convertToSassValue(Object value) {
    if (null == value) {
      return new SassNull();
    }

    if (value instanceof SassValue) {
      return (SassValue) value;
    }

    Class cls = value.getClass();

    if (boolean.class.isAssignableFrom(cls) || value instanceof Boolean) {
      return new SassBoolean((Boolean) value);
    }

    if (
        byte.class.isAssignableFrom(cls)
            || short.class.isAssignableFrom(cls)
            || int.class.isAssignableFrom(cls)
            || long.class.isAssignableFrom(cls)
            || float.class.isAssignableFrom(cls)
            || double.class.isAssignableFrom(cls)
            || value instanceof Number
        ) {
      return new SassNumber(((Number) value).doubleValue(), "");
    }

    if (char.class.isAssignableFrom(cls) || value instanceof CharSequence) {
      return new SassString(value.toString());
    }

    if (value instanceof Collection) {
      return new SassList(
          ((Collection<?>) value)
              .stream()
              .map(TypeUtils::convertToSassValue)
              .collect(Collectors.toList())
      );
    }

    if (value instanceof Map) {
      SassMap map = new SassMap();

      for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
        String key = entry.getKey().toString();
        SassValue item = TypeUtils.convertToSassValue(entry.getValue());

        map.put(key, item);
      }

      return map;
    }

    if (value instanceof Exception) {
      return new SassError(((Exception) value).getMessage());
    }

    return new SassError(
        String.format(
            "Could not convert object of type %s into a sass value",
            value.getClass().toString()
        )
    );
  }
}

@@ -22,16 +22,119 @@
 .type;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A%0Aimport java.io.PrintWriter;%0Aimport java.io.StringWriter;%0A
 import j
@@ -1887,78 +1887,431 @@
 eof 
-Exception) %7B%0A      return new SassError(((Exception) value).getMessage
+Throwable) %7B%0A      Throwable throwable = (Throwable) value;%0A      StringWriter stringWriter = new StringWriter();%0A      PrintWriter printWriter = new PrintWriter(stringWriter);%0A%0A      String message = throwable.getMessage();%0A      if (StringUtils.isNotEmpty(message)) %7B%0A        printWriter.append(message).append(%22%5Cn%22);%0A      %7D%0A      throwable.printStackTrace(printWriter);%0A%0A      return new SassError(stringWriter.toString
 ());

Convert exceptions to SassErrors including a stack trace.