Exception and error management

This feature of HTML Channel controls the errors and exceptions that have not been managed by the operations.

The following standard functionality is provided:

In the server-side toolkit configuration file (btt.xml), under the HtmlClient keyed collection, the attribute errorPage is the name of a JSP that is displayed when a system exception occurs. When this page is displayed by the presentation handler, the exception itself will be saved in the request under the key name "exception" (actually the key name is saved symbolically as com.ibm.btt.cs.html.HtmlConstants.EXCEPTION). The JSP can use the JSP Context Services bean to check for errors that were detected during the data validation process. The JSP can then retrieve and manage the specific data field error.

Advanced error handling

Handle Error 404 or 500
Background

Normally, the exceptions are caught by the BTT channels. BTT channels would put these exceptions into the attributes of "request.getAttribute(HtmlConstants.EXCEPTION)". So you could get the exception object through the key word "dse_exception" which is the value of "HtmlConstants.Exception".

But in some situations when BTT are not capable of catching some errors or exceptions, these will be thrown into the http server or web server. For this kind of exceptions, the "request.getAttribute(HtmlConstants.EXCEPTION)" will return null. It will return the error code like 404 and 500.

Process
  1. Configure Web.xml. You need to add the following code to web.xml:
    <error-page>
    <error-code>404</error-code>
    <location>/jsp/errorpage.jsp</location>
    </error-page>
    <error-page>
    <error-code>500</error-code>
    <location>/jsp/errorpage.jsp</location>
    </error-page>
    <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/jsp/errorpage.jsp</location>
    </error-page
  2. Create a jsp file named errorpage.jsp in the /jsp directory. Then there are two approaches to get the exception:
    • through consistent attributes.
      For example,

      Error Code:<%=request.getAttribute("javax.servlet.error.status_code")%> <br>
      Info : <%=request.getAttribute("javax.servlet.error.message")%> <br>
      Exception : <%=request.getAttribute("javax.servlet.error.exception_type")%> <br>

    • through one internal object exception which is like the request, response object in JSP. You could use the object directly without external declaration.
      Notice that if you want to use the exception in your code, you need to
      1. tell the web server that this JSP page is an error page by setting the "isErrorPage" with true.<%@ page isErrorPage="true" %>
      2. add the following code:
        if(null != exception){
        	out.println("An exception was thrown:" + exception.getClass());
        	out.println("Exception message:");
        	out.println(exception.getMessage());
        	out.println("Exception stack trace:");  
            exception.printStackTrace();
            ByteArrayOutputStream ostr = new ByteArrayOutputStream();
            exception.printStackTrace(new PrintStream(ostr));
            out.print(ostr);
        }
Reference: errorpage.jsp sample code
<%@page import="com.ibm.btt.base.FunctionalErrorTraceHelper"%>
<%@page import="com.ibm.btt.cs.html.HtmlConstants"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<<DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Error Page</title>
</head>
<body>
<tt style="color: red">
An internal error has occurred. Please review the traces.
<%
if (null != request.getAttribute(HtmlConstants.EXCEPTION)) {
Exception e = (Exception) request
.getAttribute(HtmlConstants.EXCEPTION);
StringBuffer sb = new StringBuffer();
sb.append("<hr/>");
sb.append("Detailed exception message is:<br/>");
if (null != e.getMessage()) {
sb.append(e.getMessage().replaceAll("<","< ">.replaceAll(">"," >"));
}
out.println(sb.toString());
// Show additional info if possible
String[] msgs = FunctionalErrorTraceHelper.getMessagesForException(e);
if (msgs.length > 0) {
out.println("<br/><br/>Additional error information:<br/>");
for (String msg : msgs) {
out.println("&nbsp;&nbsp;&nbsp;&nbsp;" + msg + "<br/>");
 } 
} 
} else if (null != request.getAttribute("javax.servlet.error.status_code")) {  
out.println("<br/>"); 
out.println("Error Code: " + request.getAttribute("javax.servlet.error.status_code") + "<br/>"); 
out.println("Error Info : " + request.getAttribute("javax.servlet.error.message") + "<br/>");
out.println("Exception : " + request.getAttribute("javax.servlet.error.exception_type") + "<br/>"); 
}  
%>  
</tt> 
</body> 
</html>