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:
12: private String message;
13:
14: public ErrorResponder(Exception e) {
15: exception = e;
16: }
17:
18: public ErrorResponder(String message) {
19: this .message = message;
20: }
21:
22: public Response makeResponse(FitNesseContext context,
23: Request request) throws Exception {
24: SimpleResponse response = new SimpleResponse(400);
25: HtmlPage html = context.htmlPageFactory.newPage();
26: HtmlUtil.addTitles(html, "Error Occured");
27: if (exception != null)
28: html.main.add("<pre>" + makeExceptionString(exception)
29: + "</pre>");
30: if (message != null)
31: html.main.add(makeErrorMessage());
32: response.setContent(html.html());
33:
34: return response;
35: }
36:
37: public static String makeExceptionString(Exception e) {
38: StringBuffer buffer = new StringBuffer();
39: buffer.append(e.toString()).append("\n");
40: StackTraceElement[] stackTreace = e.getStackTrace();
41: for (int i = 0; i < stackTreace.length; i++)
42: buffer.append("\t" + stackTreace[i]).append("\n");
43:
44: return buffer.toString();
45: }
46:
47: public HtmlTag makeErrorMessage() {
48: HtmlTag tag = HtmlUtil.makeDivTag("centered");
49: tag.add(message);
50: return tag;
51: }
52: }
|