KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > error > HtmlErrorHandler


1 /*
2 Copyright (c) 2003 eInnovation Inc. All rights reserved
3
4 This library is free software; you can redistribute it and/or modify it under the terms
5 of the GNU Lesser General Public License as published by the Free Software Foundation;
6 either version 2.1 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the GNU Lesser General Public License for more details.
11 */

12
13 package com.openedit.error;
14
15 import java.io.IOException JavaDoc;
16 import java.io.Writer JavaDoc;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20
21 import com.openedit.OpenEditException;
22 import com.openedit.WebPageRequest;
23 import com.openedit.generators.Output;
24 import com.openedit.page.Page;
25 import com.openedit.page.PageRequestKeys;
26 import com.openedit.page.PageStreamer;
27
28
29 /**
30  * DOCUMENT ME!
31  *
32  * @author cburkey
33  */

34 public class HtmlErrorHandler implements ErrorHandler
35 {
36     protected String JavaDoc fieldPathToErrorFile;
37     private static final Log log = LogFactory.getLog(HtmlErrorHandler.class);
38
39
40     /**
41      * @see org.jpublish.ErrorHandler#handleError(JPublishError)
42      */

43     public boolean handleError(Exception JavaDoc error, WebPageRequest context )
44     {
45         if (context != null)
46         {
47             try
48             {
49                 if ( !(error instanceof OpenEditException))
50                 {
51                     error = new OpenEditException(error); //we need the toStacktrace method
52
}
53                 if( !context.hasRedirected() && context.getResponse() != null)
54                 {
55                     try
56                     {
57                         context.getResponse().setStatus(500);
58                     }
59                     catch( Exception JavaDoc ex)
60                     {
61                         //ignored
62
log.info("Ignored:" + ex);
63                     }
64                 }
65                 if( log.isDebugEnabled())
66                 {
67                     error.printStackTrace();
68                 }
69                 
70                 context.putPageValue("oe-exception", error); //must be a top level thing since we create a new context
71
PageStreamer pages = (PageStreamer)context.getPageValue(PageRequestKeys.PAGES);
72                 Page errorPage = pages.getPage("/openedit/errorpage.html");
73                 if ( !errorPage.exists() )
74                 {
75                     //do nothing
76
log.error("No error page found" + errorPage.getPath());
77                     return false;
78                 }
79                 else
80                 {
81                     Writer JavaDoc out = context.getWriter();
82                     errorPage.generate(context,new Output(out, null));
83                     out.flush();
84                 }
85             } catch ( Exception JavaDoc ex)
86             {
87                 //Do not throw an error here is it will be infinite
88
log.error( ex);
89                 ex.printStackTrace();
90                 try
91                 {
92                     context.getWriter().write("Check error logs: " + ex);
93                     //throw new OpenEditRuntimeException(ex);
94
}
95                 catch (Throwable JavaDoc ex1)
96                 {
97                     log.error( ex1 );
98                 }
99             }
100             return true;
101         }
102         return false;
103     }
104     
105
106 }
107
Popular Tags