c1d72bd8d96bcaa8c3fc35434b66ae707275ee2f
[frank.git] / prod / net / jaekl / frank / ErrorHandler.java
1 package net.jaekl.frank;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.PrintStream;
5 import java.io.PrintWriter;
6 import java.util.Locale;
7
8 import net.jaekl.qd.http.InvalidResponseException;
9
10 public class ErrorHandler {
11         static final String JAVASCRIPT =
12                         "<SCRIPT>\n" +
13                         "  var show_hide = function() {\n" +
14                         "  var theDiv = document.getElementById('details');\n" +
15                         "  var theButton = document.getElementById('details_btn');\n" +
16                         "  if (theDiv.style.display === 'block' || theDiv.style.display === '') {\n" +
17                         "    theDiv.style.display = 'none';\n" +
18                         "  theButton.value = 'Show details';\n" +
19                         "  }\n" +
20                         "  else {\n" +
21                         "    theDiv.style.display = 'block';\n" +
22                         "    theButton.value = 'Hide details';\n" +
23                         "  }\n" +
24                         "}\n" +
25                         "</SCRIPT>";
26         
27         void writeScript(PrintWriter pw) {
28                 pw.println(JAVASCRIPT);
29         }
30         
31         void explain(PrintWriter pw, Throwable t, FrankBundle bundle) {
32                 Throwable cause = t;
33                 if (t instanceof FrankException) {
34                         if (null != t.getCause()) {
35                                 cause = t.getCause();
36                         }
37                 }
38                 
39                 pw.println("<P>");
40                 if (cause instanceof InvalidResponseException) {
41                         InvalidResponseException ire = (InvalidResponseException)cause;
42                         
43                         pw.println(bundle.get(FrankBundle.INVALID_RESPONSE));
44                         pw.println("<P ID=\"errtable\">");
45                         pw.println("  <TABLE>");
46                         pw.println("    <TR><TD CLASS=\"head\">" + bundle.get(FrankBundle.URL_CONTACTED) 
47                                         + "</TD><TD>" + ire.getUrl() + "</TD></TR>");
48                         pw.println("    <TR CLASS=\"alt\"><TD CLASS=\"head\">" + bundle.get(FrankBundle.REQUEST_MADE) 
49                                         + "</TD><TD>" + ire.getMethod() + "</TD></TR>");
50                         pw.println("    <TR><TD CLASS=\"head\">" + bundle.get(FrankBundle.ANSWER_RECEIVED) 
51                                         + "</TD><TD>" + ire.getResponse() + "</TD></TR>");
52                         pw.println("  </TABLE>");
53                         pw.println("</P>");
54                         pw.println("<P>" + bundle.get(FrankBundle.MAYBE_SERVER_PROBLEM) + "</P>");
55                 }
56                 else {
57                         pw.println(bundle.get(FrankBundle.UNEXPECTED_EXCEPTION));
58                 }
59                 pw.println("</P>");
60         }
61         
62         void writeErrorPage(PrintWriter pw, Throwable t, Locale locale) {
63                 Style style = new Style();
64                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
65                 PrintStream ps = new PrintStream(baos);
66                 FrankBundle bundle = FrankBundle.getInst(locale);
67                 
68                 pw.println("<HTML><HEAD>");
69                 pw.println("<TITLE>" + 
70                                 bundle.get(FrankBundle.FRANK) + ": " +
71                                 bundle.get(FrankBundle.ERROR_PAGE) + 
72                                 "</TITLE>");
73                 style.writeStyle(pw);
74                 writeScript(pw);
75                 pw.println("</HEAD>");
76                 
77                 pw.println("<BODY>");
78                 pw.println("<TABLE ID=\"errhead\" WIDTH=\"100%\"><TR><TD>" +
79                                 bundle.get(FrankBundle.FRANK) + ": " +
80                                 bundle.get(FrankBundle.UNEXPECTED_ERROR) + 
81                                 "</TD></TR></TABLE>");
82                 
83                 explain(pw, t, bundle);
84                 
85                 // Note that, if we cared about security, we would log this stack trace to a
86                 // server log, and only report a cross-reference to the log file back to the 
87                 // end user's browser, to avoid potentially exposing internal info that we 
88                 // don't want to share.
89                 // At least at this point, we don't care (that much), and trade off a 
90                 // potential information leak in favour of reducing our code complexity
91                 // and the administrator's workload.
92                 
93                 pw.println("<P><INPUT TYPE=\"button\" ID=\"details_btn\" VALUE=\"Show details\" ONCLICK=\"show_hide();\"/></P>");
94                 pw.println("<P>");
95                 pw.println("<DIV ID=\"details\" STYLE=\"display: none;\"><PRE>");
96                 
97                 t.printStackTrace(ps);
98                 String stackTrace = baos.toString(); 
99                 pw.println(stackTrace);
100                                 
101                 pw.println("</PRE>\n</P>\n</DIV>");
102                 pw.println("<P>Click <A HREF=\"/\">here</A> to return to the main page.</P>");
103                 pw.println("</BODY></HTML>");
104         }
105 }