01: /*
02: Copyright (c) 2003 eInnovation Inc. All rights reserved
03:
04: This library is free software; you can redistribute it and/or modify it under the terms
05: of the GNU Lesser General Public License as published by the Free Software Foundation;
06: either version 2.1 of the License, or (at your option) any later version.
07:
08: This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
09: 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;
16: import java.io.Writer;
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: * DOCUMENT ME!
30: *
31: * @author cburkey
32: */
33: public class HtmlErrorHandler implements ErrorHandler {
34: protected String fieldPathToErrorFile;
35: private static final Log log = LogFactory
36: .getLog(HtmlErrorHandler.class);
37:
38: /**
39: * @see org.jpublish.ErrorHandler#handleError(JPublishError)
40: */
41: public boolean handleError(Exception error, WebPageRequest context) {
42: if (context != null) {
43: try {
44: if (!(error instanceof OpenEditException)) {
45: error = new OpenEditException(error); //we need the toStacktrace method
46: }
47: if (!context.hasRedirected()
48: && context.getResponse() != null) {
49: try {
50: context.getResponse().setStatus(500);
51: } catch (Exception ex) {
52: //ignored
53: log.debug("Ignored:" + ex);
54: }
55: }
56: if (log.isDebugEnabled()) {
57: error.printStackTrace();
58: }
59: context.putPageValue("editPath", context.getPage()
60: .getPath());
61: context.putPageValue("oe-exception", error); //must be a top level thing since we create a new context
62: PageStreamer pages = (PageStreamer) context
63: .getPageValue(PageRequestKeys.PAGES);
64: Page errorPage = pages
65: .getPage("/openedit/errorpage.html");
66: if (!errorPage.exists()) {
67: //do nothing
68: log.error("No error page found"
69: + errorPage.getPath());
70: return false;
71: } else {
72: Writer out = context.getWriter();
73: errorPage.generate(context, new Output(out, null));
74: out.flush();
75: }
76: } catch (Exception ex) {
77: //Do not throw an error here is it will be infinite
78: log.error(ex);
79: ex.printStackTrace();
80: try {
81: context.getWriter()
82: .write("Check error logs: " + ex);
83: //throw new OpenEditRuntimeException(ex);
84: } catch (Throwable ex1) {
85: log.error(ex1);
86: }
87: }
88: return true;
89: }
90: return false;
91: }
92:
93: }
|