ZK Exception Handling and Error Popup

Once I had some basic functionality in my mini accounting system that I’m writing (for research purposes), one of my goals was to have an adequate error display for the user, that was the same every time.  Suffice it to say, I was completely thrilled to find out that ZK could do exactly what I wanted to do, and I didn’t have to write an once of code.  All I did was write some ZUL, which wrapped a JSP inside of a ZK window.  I share how I did that in this post.

Configuration

The first thing to do is to configure ZK to handle your unhandled exceptions, and target a ZUL page for rendering.  This is done through the zk.xml file.

<error-page>
 <exception-type>java.lang.Throwable</exception-type>
 <location>/error.zul</location>
</error-page>

ZK error.zul

The second thing to do is create an error.zul page, which I pulled mostly from the ZK Developers Guide.  I added the bit where I include my error.jsp file.

<?xml version="1.0" encoding="UTF-8"?>
<zk xmlns="http://www.zkoss.org/2005/zul"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd">
  <window mode="modal" sizable="true"
          title="Error ${requesScope['javax.servlet.error.status_code']}">
    <vbox>
      <hbox>
        <include src="/jsp/error.jsp"/>
      </hbox>
      <hbox style="margin-left:auto; margin-right:auto; text-align: center;">
        <button label="Okay" onClick="spaceOwner.detach()"/>
        <button label="Reload Page" onClick="Executions.sendRedirect(null)"/>
      </hbox>
    </vbox>
  </window>
</zk>

JSP error.jsp

I basically pulled this error page from the standard error page that I normally use, and tailored it a bit.

<%@ page import="java.io.PrintWriter" %>
<%@ page import="java.io.StringWriter" %>
<%@ page import="com.example.util.Util" %>
<%@ page isErrorPage="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="ce" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/string-1.1" prefix="st" %>
<!-- BEGIN error.jsp -->
<div class="error">
  <p class="message"><%=exception.getMessage()%></p>
 
  <p>An error has occured. If you believe this error is a system problem, and you
  continue to get this error, please contact a special person via
    <a href="mailto:'me@example.com' ">email</a>
  </p>
 
  <ce:if test="<%=Util.getBooleanItem(\"/debug\")%>">
    <%
      StringWriter sw = new StringWriter();
      PrintWriter pw = new PrintWriter(sw);
      exception.printStackTrace(pw);
    %>
    <ce:set var="exceptionStack"><%=sw.toString()%>
    </ce:set>
    <div class="debug">
      Message : <%=exception.getMessage()%>
      Exception : <%=exception.getClass().getName()%>
      Stack : ${fn:escapeXml(exceptionStack)}
    </div>
  </ce:if>
</div>
<!-- END error.jsp -->

Sample Error Page

And, we have the result;  it looks quite nice.  If debug were enabled, it would have my stack trace displayed in a wrap-able fixed width font ; providing much easier diagnostics during development.

ZK Sample Error Page

3 thoughts on “ZK Exception Handling and Error Popup

  1. Senthil

    Great. I need some more information to be shown in the error popup for the development people. I mean , can we include one more button say “Details”, when we click on details, can we show the stack of messages

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *