use of freemarker.core.ParseException in project ninja by ninjaframework.
the class TemplateEngineFreemarker method throwRenderingException.
public void throwRenderingException(Context context, Result result, Exception cause, String knownTemplateSourcePath) {
// a more useful ParseException
if (cause instanceof IOException && cause.getCause() != null && cause.getCause() instanceof ParseException) {
cause = (ParseException) cause.getCause();
}
if (cause instanceof TemplateNotFoundException) {
// inner cause will be better to display
throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker template not found", knownTemplateSourcePath, -1);
} else if (cause instanceof TemplateException) {
TemplateException te = (TemplateException) cause;
String templateSourcePath = te.getTemplateSourceName();
if (templateSourcePath == null) {
templateSourcePath = knownTemplateSourcePath;
}
throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker render exception", templateSourcePath, te.getLineNumber());
} else if (cause instanceof ParseException) {
ParseException pe = (ParseException) cause;
String templateSourcePath = pe.getTemplateName();
if (templateSourcePath == null) {
templateSourcePath = knownTemplateSourcePath;
}
throw new RenderingException(cause.getMessage(), cause, result, "FreeMarker parser exception", templateSourcePath, pe.getLineNumber());
}
// fallback to throwing generic rendering exception
throw new RenderingException(cause.getMessage(), cause, result, knownTemplateSourcePath, -1);
}
use of freemarker.core.ParseException in project moco by dreamhead.
the class TemplateResourceReader method readFor.
@Override
public MessageContent readFor(final Optional<? extends Request> request) {
if (!request.isPresent()) {
throw new IllegalStateException("Request is required to render template");
}
MessageContent content = this.template.readFor(request);
try {
Template targetTemplate = createTemplate(content);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Writer writer = new OutputStreamWriter(stream);
targetTemplate.process(variables(request.get()), writer);
return content().withContent(stream.toByteArray()).build();
} catch (ParseException e) {
logger.error("Fail to parse template: {}", content.toString());
throw new MocoException(e);
} catch (IOException e) {
throw new MocoException(e);
} catch (TemplateException e) {
throw new MocoException(e);
}
}
use of freemarker.core.ParseException in project perun by CESNET.
the class PerunNotifTemplateManagerImpl method validateTemplateMessage.
private void validateTemplateMessage(PerunNotifTemplateMessage message) throws InternalErrorException, TemplateMessageSyntaxErrorException {
String templateName = Integer.toString(message.getTemplateId());
Locale locale = message.getLocale();
try {
Template freeMarkerTemplate = this.configuration.getTemplate(templateName + "_" + locale.getLanguage(), locale);
} catch (ParseException ex) {
throw new TemplateMessageSyntaxErrorException(message, ex);
} catch (IOException ex) {
// template not found
throw new InternalErrorException("FreeMarker Template internal error.", ex);
}
}
Aggregations