1 package net.jaekl.frank;
3 import java.io.ByteArrayOutputStream;
4 import java.io.PrintStream;
5 import java.io.PrintWriter;
6 import java.util.Locale;
8 import net.jaekl.qd.http.InvalidResponseException;
10 public class ErrorHandler {
11 static final String JAVASCRIPT =
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" +
21 " theDiv.style.display = 'block';\n" +
22 " theButton.value = 'Hide details';\n" +
27 void writeScript(PrintWriter pw) {
28 pw.println(JAVASCRIPT);
31 void explain(PrintWriter pw, Throwable t, FrankBundle bundle) {
33 if (t instanceof FrankException) {
34 if (null != t.getCause()) {
40 if (cause instanceof InvalidResponseException) {
41 InvalidResponseException ire = (InvalidResponseException)cause;
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>");
54 pw.println("<P>" + bundle.get(FrankBundle.MAYBE_SERVER_PROBLEM) + "</P>");
57 pw.println(bundle.get(FrankBundle.UNEXPECTED_EXCEPTION));
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);
68 pw.println("<HTML><HEAD>");
69 pw.println("<TITLE>" +
70 bundle.get(FrankBundle.FRANK) + ": " +
71 bundle.get(FrankBundle.ERROR_PAGE) +
75 pw.println("</HEAD>");
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>");
83 explain(pw, t, bundle);
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.
93 pw.println("<P><INPUT TYPE=\"button\" ID=\"details_btn\" VALUE=\"Show details\" ONCLICK=\"show_hide();\"/></P>");
95 pw.println("<DIV ID=\"details\" STYLE=\"display: none;\"><PRE>");
97 t.printStackTrace(ps);
98 String stackTrace = baos.toString();
99 pw.println(stackTrace);
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>");