Error Handling"
Line 62: | Line 62: | ||
=Error Handling When Serving AU Requests= | =Error Handling When Serving AU Requests= | ||
+ | |||
+ | If an uncaught exception is thrown when serving an AU request (aka., an Ajax request; such as caused by an event listener), it is handled by the ZK Update Engine. By default, it simply shows up an error message to indicate the error. | ||
+ | |||
+ | For example, suppose we have the following code: | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <button label="Cause Error" onClick='throw new NullPointerException("Unknown Value")'/> | ||
+ | </source> | ||
+ | |||
+ | Then, if you click the button, the following error message will be shown. | ||
+ | |||
+ | [[Image:Exception-au.png]] | ||
+ | |||
+ | You can customize the error handling by specifying the error page in <tt>WEB-INF/zk.xml</tt> as described in [[ZK Configuration Reference/zk.xml/The error-page Element|ZK Configuration Reference]]. For example, | ||
+ | |||
+ | <source lang="xml"> | ||
+ | <!-- zk.xml --> | ||
+ | <error-page> | ||
+ | <exception-type>java.lang.Throwable</exception-type> | ||
+ | <location>/WEB-INF/sys/error.zul</location> | ||
+ | </error-page> | ||
+ | </source> | ||
+ | |||
+ | Then, when an error occurs in an event listener, the ZK Update Engine creates a dialog by use of the error page you specified, /error/error.zul. | ||
+ | |||
+ | Like error handling in loading a ZUML page, you can specify multiple <error-page> elements. Each of them is associated with a different exception type (the value of <exception-type> element). When an error occurs, ZK will search the error pages one-by-one until the exception type matches. | ||
+ | |||
+ | In addition, ZK passes a set of request attributes to the error page to describe what happens. These attribute are as follows. | ||
+ | |||
=Version History= | =Version History= | ||
{{LastUpdated}} | {{LastUpdated}} |
Revision as of 03:33, 7 December 2010
Here we describe how to handle errors. An error is caused by an exception that is not caught by the application. An exception might be thrown in two situations: when loading a ZUML document, and when serving an AU request (aka, an Ajax request).
Error Handling When Loading ZUML Documents
If an un-caught exception is thrown when loading a ZUML document, it is handled directly by the Web server. In other words, the handling is no different from other servlets.
By default, the Web server displays an error page showing the error message and stack trace. For example,
You can customize the error handling by specifying the error page in WEB-INF/web.xml as follows[1].
<!-- WEB-INF/web.xml -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/WEB-INF/sys/error.zul</location>
</error-page>
Then, when an error occurs in loading a page, the Web server forwards the error page you specified, /error/error.zul. Upon forwarding, the Web server passes a set of request attributes to the error page to describe what happens. These attributes are as follows.
Request Attribute | Type |
---|---|
javax.servlet.error.status_code | java.lang.Integer |
javax.servlet.error.exception_type | java.lang.Class |
javax.servlet.error.message | java.lang.String |
javax.servlet.error.exception | java.lang.Throwable |
javax.servlet.error.request_uri | java.lang.String |
javax.servlet.error.servlet_name | java.lang.String |
Then, in the error page, you can display your custom information by use of these attributes. For example,
<window title="Error ${requestScope['javax.servlet.error.status_code']}">
Cause: ${requestScope['javax.servlet.error.message']}
</window>
Tips:
- The error page can be any kind of servlets. In addition to ZUML, you can use JSP or whatever servlet you preferred.
- ↑ Please refer to Java Servlet Specification for more details.
Error Handling When Serving AU Requests
If an uncaught exception is thrown when serving an AU request (aka., an Ajax request; such as caused by an event listener), it is handled by the ZK Update Engine. By default, it simply shows up an error message to indicate the error.
For example, suppose we have the following code:
<button label="Cause Error" onClick='throw new NullPointerException("Unknown Value")'/>
Then, if you click the button, the following error message will be shown.
You can customize the error handling by specifying the error page in WEB-INF/zk.xml as described in ZK Configuration Reference. For example,
<!-- zk.xml -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/WEB-INF/sys/error.zul</location>
</error-page>
Then, when an error occurs in an event listener, the ZK Update Engine creates a dialog by use of the error page you specified, /error/error.zul.
Like error handling in loading a ZUML page, you can specify multiple <error-page> elements. Each of them is associated with a different exception type (the value of <exception-type> element). When an error occurs, ZK will search the error pages one-by-one until the exception type matches.
In addition, ZK passes a set of request attributes to the error page to describe what happens. These attribute are as follows.
Version History
Version | Date | Content |
---|---|---|