Exception handling in Widget.doMarkup() for exceptions not caught in the widget's process() method.

This commit is contained in:
rjy7 2010-11-17 16:22:05 +00:00
parent 400977d912
commit 2ec80768e6
2 changed files with 21 additions and 6 deletions

View file

@ -90,6 +90,8 @@ public class LoginWidget extends Widget {
} }
} catch (Exception e) { } catch (Exception e) {
log.error(e); log.error(e);
// This widget should display an error message rather than throwing the exception
// up to the doMarkup() method, which would result in no display.
values = showError(e); values = showError(e);
} }
values.put("urls", urls); values.put("urls", urls);

View file

@ -55,9 +55,17 @@ public abstract class Widget {
HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request"); HttpServletRequest request = (HttpServletRequest) env.getCustomAttribute("request");
ServletContext context = (ServletContext) env.getCustomAttribute("context"); ServletContext context = (ServletContext) env.getCustomAttribute("context");
WidgetTemplateValues values = process(env, params, request, context); WidgetTemplateValues values = null;
try {
values = process(env, params, request, context);
} catch (Exception e) {
log.error(e, e);
}
// The widget process() method may determine that nothing should display for the widget: // The widget process() method may determine that nothing should display for the widget:
// for example, the login widget doesn't display if the user is already logged in. // for example, the login widget doesn't display if the user is already logged in. This also
// applies if process() threw an error.
if (values == null) { if (values == null) {
return ""; return "";
} }
@ -92,7 +100,7 @@ public abstract class Widget {
// } // }
protected abstract WidgetTemplateValues process(Environment env, Map params, protected abstract WidgetTemplateValues process(Environment env, Map params,
HttpServletRequest request, ServletContext context); HttpServletRequest request, ServletContext context) throws Exception;
private String processMacroToString(Environment env, String widgetName, Macro macro, Map<String, Object> map) { private String processMacroToString(Environment env, String widgetName, Macro macro, Map<String, Object> map) {
StringWriter out = new StringWriter(); StringWriter out = new StringWriter();
@ -108,8 +116,8 @@ public abstract class Widget {
// if it's already there or else add it. Leave this for later. // if it's already there or else add it. Leave this for later.
Template template = new Template("widget", new StringReader(templateString), env.getConfiguration()); Template template = new Template("widget", new StringReader(templateString), env.getConfiguration());
template.process(map, out); template.process(map, out);
} catch (Throwable th) { } catch (Exception e) {
log.error("Could not process widget " + widgetName, th); log.error("Could not process widget " + widgetName, e);
} }
String output = out.toString(); String output = out.toString();
log.debug("Macro output: " + output); log.debug("Macro output: " + output);
@ -163,8 +171,13 @@ public abstract class Widget {
public String getMacroName() { public String getMacroName() {
return this.macroName; return this.macroName;
}
}
protected class WidgetProcessingException extends Exception {
WidgetProcessingException(String message) {
super(message);
} }
} }
} }