Java Code Examples for com.hubspot.jinjava.interpret.JinjavaInterpreter#addError()

The following examples show how to use com.hubspot.jinjava.interpret.JinjavaInterpreter#addError() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JinjavaInterpreterResolver.java    From jinjava with Apache License 2.0 5 votes vote down vote up
private static DateTimeFormatter getFormatter(
  JinjavaInterpreter interpreter,
  FormattedDate d
) {
  if (!StringUtils.isBlank(d.getFormat())) {
    try {
      return StrftimeFormatter.formatter(
        d.getFormat(),
        interpreter.getConfig().getLocale()
      );
    } catch (IllegalArgumentException e) {
      interpreter.addError(
        new TemplateError(
          ErrorType.WARNING,
          ErrorReason.SYNTAX_ERROR,
          ErrorItem.OTHER,
          e.getMessage(),
          null,
          interpreter.getLineNumber(),
          -1,
          null,
          BasicTemplateErrorCategory.UNKNOWN_DATE,
          ImmutableMap.of(
            "date",
            d.getDate().toString(),
            "exception",
            e.getMessage(),
            "lineNumber",
            String.valueOf(interpreter.getLineNumber())
          )
        )
      );
    }
  }

  return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
}
 
Example 2
Source File: JinjavaInterpreterResolver.java    From jinjava with Apache License 2.0 5 votes vote down vote up
private static Locale getLocale(JinjavaInterpreter interpreter, FormattedDate d) {
  if (!StringUtils.isBlank(d.getLanguage())) {
    try {
      return LocaleUtils.toLocale(d.getLanguage());
    } catch (IllegalArgumentException e) {
      interpreter.addError(
        new TemplateError(
          ErrorType.WARNING,
          ErrorReason.SYNTAX_ERROR,
          ErrorItem.OTHER,
          e.getMessage(),
          null,
          interpreter.getLineNumber(),
          -1,
          null,
          BasicTemplateErrorCategory.UNKNOWN_LOCALE,
          ImmutableMap.of(
            "date",
            d.getDate().toString(),
            "exception",
            e.getMessage(),
            "lineNumber",
            String.valueOf(interpreter.getLineNumber())
          )
        )
      );
    }
  }

  return Locale.US;
}
 
Example 3
Source File: JoinFilter.java    From jinjava with Apache License 2.0 4 votes vote down vote up
@Override
public Object filter(Object var, JinjavaInterpreter interpreter, String... args) {
  LengthLimitingStringBuilder stringBuilder = new LengthLimitingStringBuilder(
    interpreter.getConfig().getMaxStringLength()
  );

  String separator = "";
  if (args.length > 0) {
    separator = args[0];
  }

  String attr = null;
  if (args.length > 1) {
    attr = args[1];
  }

  ForLoop loop = ObjectIterator.getLoop(var);
  boolean first = true;
  while (loop.hasNext()) {
    Object val = loop.next();

    if (attr != null) {
      val = interpreter.resolveProperty(val, attr);
    }

    try {
      if (!first) {
        stringBuilder.append(separator);
      } else {
        first = false;
      }
      stringBuilder.append(Objects.toString(val, ""));
    } catch (OutputTooBigException ex) {
      interpreter.addError(
        new TemplateError(
          ErrorType.WARNING,
          ErrorReason.OTHER,
          ErrorItem.FILTER,
          String.format(
            "Result of %s filter has been truncated to the max String length of %d",
            getName(),
            interpreter.getConfig().getMaxStringLength()
          ),
          null,
          interpreter.getLineNumber(),
          interpreter.getPosition(),
          ex
        )
      );

      return stringBuilder.toString();
    }
  }

  return stringBuilder.toString();
}