updated for SML/NJ 110.72
[bpt/mlt.git] / src / lib / main.sml
index b058cce..9ffaf5c 100644 (file)
@@ -29,7 +29,9 @@ struct
            val cgiFields = Cgi.cgi_fieldnames ()
            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,12 +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 Fail msg => (print "Status: 500\nContent-type: text/plain\n\nFatal error: \n\n";
-                               print msg;
-                               OS.Process.failure)
-                | 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