blob: b1f040395410f93693009826ab83edc4a1dfb6b8 [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>6.8.&nbsp;Error Handling</title><link rel="stylesheet" href="css/stylesheet.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.75.0"><link rel="home" href="index.html" title="Apache Click"><link rel="up" href="ch06.html" title="Chapter&nbsp;6.&nbsp;Best Practices"><link rel="prev" href="ch06s07.html" title="6.7.&nbsp;Logging"><link rel="next" href="ch06s09.html" title="6.9.&nbsp;Performance"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">6.8.&nbsp;Error Handling</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch06s07.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;6.&nbsp;Best Practices</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ch06s09.html">Next</a></td></tr></table><hr></div><div class="sect1" title="6.8.&nbsp;Error Handling"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="error-handling"></a>6.8.&nbsp;Error Handling</h2></div></div></div><p> In Click unhandled errors are directed to the
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/util/ErrorPage.html" target="_blank">ErrorPage</a>
for display. If applications require additional error handling they can create
and register a custom error page in <code class="filename">WEB-INF/click.xml</code>.
For example:
</p><pre class="programlisting"><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag">&lt;pages</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">package</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"com.mycorp.page"</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">autobinding</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"annotation"</span><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag">/&gt;</span>
<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag">&lt;page</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">path</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"click/error.htm"</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="atn">classname</span>=<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="pln">"com.mycorp.page.ErrorPage"</span><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag">/&gt;</span>
<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="tag">&lt;/pages&gt;</span></pre><p>Generally applications handle transactional errors using service layer code
or via a servlet
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/Filter.html" target="_blank">Filter</a>
and would not need to include error handling logic in an error page.
</p><p>Potential uses for a custom error page include custom logging. For example
if an application requires unhandled errors to be logged to an application
log (rather than System.out) then a custom
<a xmlns:fo="http://www.w3.org/1999/XSL/Format" class="external" href="../../click-api/org/apache/click/util/ErrorPage.html" target="_blank">ErrorPage</a>
could be configured. An example <code class="classname">ErrorPage</code> error logging
page is provided below:
</p><pre class="programlisting"><span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">package</span> com.mycorp.page.ErrorPage;
..
<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">class</span> ErrorPage <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">extends</span> org.apache.click.util.ErrorPage {
<span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">public</span> <span xmlns:fo="http://www.w3.org/1999/XSL/Format" class="kwd">void</span> onDestroy() {
Logger.getLogger(getClass()).error(getError());
}
}</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch06s07.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ch06.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ch06s09.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">6.7.&nbsp;Logging&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;6.9.&nbsp;Performance</td></tr></table></div></body></html>