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