Skip to content

Commit

Permalink
Improved: Configure stackTrace displaying on ftl rendering
Browse files Browse the repository at this point in the history
(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 7a34878
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import freemarker.template.TemplateModel; import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException; import freemarker.template.TemplateModelException;
import freemarker.template.Version; import freemarker.template.Version;
import org.apache.ofbiz.widget.model.ModelWidget;


/** /**
* FreeMarkerWorker - Freemarker Template Engine Utilities. * FreeMarkerWorker - Freemarker Template Engine Utilities.
Expand Down Expand Up @@ -108,7 +109,11 @@ public static Configuration makeConfiguration(BeansWrapper wrapper) {
newConfig.setAutoImports(freemarkerImports); newConfig.setAutoImports(freemarkerImports);
} }
newConfig.setLogTemplateExceptions(false); 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 { try {
newConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS"); newConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss.SSS");
newConfig.setSetting("number_format", "0.##########"); newConfig.setSetting("number_format", "0.##########");
Expand Down Expand Up @@ -491,16 +496,33 @@ protected URL getURL(String name) {
} }


/** /**
* OFBiz specific TemplateExceptionHandler. * OFBiz specific {@link TemplateExceptionHandler} interface.
*/ */
static class OFBizTemplateExceptionHandler implements TemplateExceptionHandler { interface OFBizTemplateExceptionHandler {
public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
/**
* {@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 { try {
out.write(te.getMessage()); out.write(te.getMessage());
Debug.logError(te, module); Debug.logError(te, module);
} catch (IOException e) { } catch (IOException e) {
Debug.logError(e, module); 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);
}
};
} }
} }
7 changes: 7 additions & 0 deletions framework/widget/config/widget.properties
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,3 +42,10 @@ widget.form.displayhelpText=Y
# first appears. # first appears.
widget.defaultNoConditionFind=N 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.