Search in sources :

Example 1 with ParseException

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);
}
Also used : TemplateException(freemarker.template.TemplateException) RenderingException(ninja.exceptions.RenderingException) TemplateNotFoundException(freemarker.template.TemplateNotFoundException) IOException(java.io.IOException) ParseException(freemarker.core.ParseException)

Example 2 with ParseException

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);
    }
}
Also used : MessageContent(com.github.dreamhead.moco.model.MessageContent) TemplateException(freemarker.template.TemplateException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(freemarker.core.ParseException) MocoException(com.github.dreamhead.moco.MocoException) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Example 3 with ParseException

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);
    }
}
Also used : TemplateMessageSyntaxErrorException(cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException) ParseException(freemarker.core.ParseException) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Template(freemarker.template.Template)

Aggregations

ParseException (freemarker.core.ParseException)3 IOException (java.io.IOException)3 Template (freemarker.template.Template)2 TemplateException (freemarker.template.TemplateException)2 MocoException (com.github.dreamhead.moco.MocoException)1 MessageContent (com.github.dreamhead.moco.model.MessageContent)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 TemplateMessageSyntaxErrorException (cz.metacentrum.perun.notif.exceptions.TemplateMessageSyntaxErrorException)1 TemplateNotFoundException (freemarker.template.TemplateNotFoundException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 RenderingException (ninja.exceptions.RenderingException)1