Better reporting of cascading exceptions
[bpt/mlt.git] / src / lib / main.sml
index 0533912..9ffaf5c 100644 (file)
@@ -27,9 +27,11 @@ struct
        let
            val _ = Cgi.init ()
            val cgiFields = Cgi.cgi_fieldnames ()
-           fun mapper name = (name, valOf (Cgi.cgi_field_string name))
+           fun mapper name = (name, Cgi.cgi_field_strings name)
        in
+           Web.addHeader ("Content-type", "text/html");
            Web.pushParams (map mapper cgiFields);
+           Templates.beforeFn ();
            case args of
                [] => (print "Status: 500\nContent-type: text/html\n\n<html><head><title>500 error</title></head><body><h2>500 error</h2>No template was specified</body></html>\n";
                       OS.Process.failure)
@@ -38,9 +40,20 @@ struct
                     NONE => (print "Status: 404\nContent-type: text/html\n\n<html><head><title>404 error</title></head><body><h2>404 error</h2>Template not found</body></html>\n";
                              OS.Process.failure)
                   | SOME f => (f ();
+                               Templates.afterFn ();
                                Web.output ();
                                OS.Process.success))
-       end handle ex => (print "Status: 500\nContent-type: text/plain\n\nAn exception!\n\n";
-                         app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory ex);
+       end handle ex => (Web.setExn ex;
+                         Web.clear ();
+                         (Templates.exnFn ();
+                          if Web.noOutput () then
+                              (print "Status: 500\nContent-type: text/plain\n\nUncaught exception:\n\n";
+                               app (fn s => print (s ^ "\n")) (SMLofNJ.exnHistory ex))
+                          else
+                              Web.output ())
+                         handle ex' => (print "Status: 500\nContent-type : text/plain\n\nUncaught exception:\n\n";
+                                        app (fn s => print ("\t" ^ s ^ "\n")) (SMLofNJ.exnHistory ex);
+                                        print "\nAnother exception raised in exception handler:\n\n";
+                                        app (fn s => print ("\t" ^ s ^ "\n")) (SMLofNJ.exnHistory ex'));
                          OS.Process.failure)
 end
\ No newline at end of file