01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.responders;
04:
05: import fitnesse.http.*;
06: import fitnesse.*;
07: import fitnesse.html.*;
08:
09: public class ErrorResponder implements Responder {
10: Exception exception;
11: private String message;
12:
13: public ErrorResponder(Exception e) {
14: exception = e;
15: }
16:
17: public ErrorResponder(String message) {
18: this .message = message;
19: }
20:
21: public Response makeResponse(FitNesseContext context,
22: Request request) throws Exception {
23: SimpleResponse response = new SimpleResponse(400);
24: HtmlPage html = context.htmlPageFactory.newPage();
25: HtmlUtil.addTitles(html, "Error Occured");
26: if (exception != null)
27: html.main.add("<pre>" + makeExceptionString(exception)
28: + "</pre>");
29: if (message != null)
30: html.main.add(makeErrorMessage());
31: response.setContent(html.html());
32:
33: return response;
34: }
35:
36: public static String makeExceptionString(Exception e) {
37: StringBuffer buffer = new StringBuffer();
38: buffer.append(e.toString()).append("\n");
39: StackTraceElement[] stackTreace = e.getStackTrace();
40: for (int i = 0; i < stackTreace.length; i++)
41: buffer.append("\t" + stackTreace[i]).append("\n");
42:
43: return buffer.toString();
44: }
45:
46: public HtmlTag makeErrorMessage() {
47: HtmlTag tag = HtmlUtil.makeDivTag("centered");
48: tag.add(message);
49: return tag;
50: }
51: }
|