svn commit: r1856524 - in /ofbiz/ofbiz-framework/trunk/framework: base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java widget/config/widget.properties

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1856524 - in /ofbiz/ofbiz-framework/trunk/framework: base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java widget/config/widget.properties

nmalin
Author: nmalin
Date: Fri Mar 29 08:28:15 2019
New Revision: 1856524

URL: http://svn.apache.org/viewvc?rev=1856524&view=rev
Log:
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

Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java
    ofbiz/ofbiz-framework/trunk/framework/widget/config/widget.properties

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java?rev=1856524&r1=1856523&r2=1856524&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/template/FreeMarkerWorker.java Fri Mar 29 08:28:15 2019
@@ -62,6 +62,7 @@ import freemarker.template.TemplateHashM
 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 final class FreeMarkerWorker {
             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 @@ public final class FreeMarkerWorker {
     }
 
     /**
-     * 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);
+            }
+        };
     }
 }

Modified: ofbiz/ofbiz-framework/trunk/framework/widget/config/widget.properties
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/widget/config/widget.properties?rev=1856524&r1=1856523&r2=1856524&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/widget/config/widget.properties (original)
+++ ofbiz/ofbiz-framework/trunk/framework/widget/config/widget.properties Fri Mar 29 08:28:15 2019
@@ -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=
\ No newline at end of file