Skip to content
Permalink
Browse files

Improved: Configure stackTrace displaying on ftl rendering

(OFBIZ-10817)

When freemarker failed to execute a template, you have on end screen rendered all java stack trace generated.

I improve this to display the stack trace only when widget is on verbose, no regression during development and when you switch to production site, in general your widget verbose is off so end user haven't stack trace and replace it by ∎ (that totally unused for them but help to detect a problem).

I also introduce two new properties on widget.properties:
    widget.freemarker.template.verbose : if your widget verbose is off and you want display the freemarker stacktrace when your template generate an exception set it to true
    widget.freemarker.template.exception.message : when you don't displaying freemarker stacktrace, you can replace it by an other message by default it use ∎ but you can set what you want, like 'ERROR', '##' or ' '

Thanks to Deepak Dixit, Jacques Le Roux, Michael Brohl and Mathieu Lirzin for their suggest

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1856524 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
nmalin committed Mar 29, 2019
1 parent ad21c41 commit 7a34878e510850d8aa83adb0519b0e6b98351725
@@ -62,6 +62,7 @@
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.Version;
import org.apache.ofbiz.widget.model.ModelWidget;

/**
* FreeMarkerWorker - Freemarker Template Engine Utilities.
@@ -108,7 +109,11 @@ public static Configuration makeConfiguration(BeansWrapper wrapper) {
newConfig.setAutoImports(freemarkerImports);
}
newConfig.setLogTemplateExceptions(false);
newConfig.setTemplateExceptionHandler(new FreeMarkerWorker.OFBizTemplateExceptionHandler());
boolean verboseTemplate = ModelWidget.widgetBoundaryCommentsEnabled(null)
|| UtilProperties.getPropertyAsBoolean("widget", "widget.freemarker.template.verbose", false);
newConfig.setTemplateExceptionHandler(verboseTemplate
? OFBizTemplateExceptionHandler.OFBIZ_DEBUG_HANDLER
: OFBizTemplateExceptionHandler.OFBIZ_DEFAULT_HANDLER);
try {
newConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
newConfig.setSetting("number_format", "0.##########");
@@ -491,16 +496,33 @@ protected URL getURL(String name) {
}

/**
* OFBiz specific TemplateExceptionHandler.
* OFBiz specific {@link TemplateExceptionHandler} interface.
*/
static class OFBizTemplateExceptionHandler implements TemplateExceptionHandler {
public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
interface OFBizTemplateExceptionHandler {

/**
* {@link TemplateExceptionHandler} that suppresses the exception and keep the rendering going on.
* It sanitizes any messages present in the stack trace prior to printing to the output writer.
*/
TemplateExceptionHandler OFBIZ_DEBUG_HANDLER = (te, env, out) -> {
try {
out.write(te.getMessage());
Debug.logError(te, module);
} catch (IOException e) {
Debug.logError(e, module);
}
}
};

/**
* {@link TemplateExceptionHandler} that suppresses the exception and replace by a generic char for quiet alert.
* As mentioned in the doc, the stack trace is still logged {@link TemplateExceptionHandler#IGNORE_HANDLER}
*/
TemplateExceptionHandler OFBIZ_DEFAULT_HANDLER = (te, env, out) -> {
try {
out.write(UtilProperties.getPropertyValue("widget", "widget.freemarker.template.exception.message",""));
} catch (IOException e) {
Debug.logError(e, module);
}
};
}
}
@@ -42,3 +42,10 @@ widget.form.displayhelpText=Y
# first appears.
widget.defaultNoConditionFind=N

# If your widget.verbose is set to false and you want display the freemarker stacktrace
# when your template generate an exception set it to true
#widget.freemarker.template.verbose=false

# When you don't displaying freemarker stacktrace, you can replace it by an other message
# by default it use ∎ but you can set what you want, like 'ERROR', '##' or ' '
#widget.freemarker.template.exception.message=

0 comments on commit 7a34878

Please sign in to comment.
You can’t perform that action at this time.