upgrade autocracy
[mdref/mdref] / mdref / ExceptionHandler.php
index d184ea08650ad423d0edd7021ce22caa3a03a5a2..1c9b7da8a60474c09f9f505e06013e1a38a4b63b 100644 (file)
@@ -4,6 +4,9 @@ namespace mdref;
 
 use http\Env as HTTP;
 
+/**
+ * mdref exception handler
+ */
 class ExceptionHandler
 {
        function __construct() {
@@ -16,7 +19,7 @@ class ExceptionHandler
                        try {
                                echo static::html($e);
                        } catch (\Exception $ignore) {
-                               HTTP::sendStatusCode(500);
+                               headers_sent() or HTTP::setResponseCode(500);
                        }
                } else {
                        throw new \Exception($msg, $e);
@@ -24,11 +27,19 @@ class ExceptionHandler
                return true;
        }
        
+       /**
+        * Format an exception as HTML and send appropriate exception info as HTTP headers
+        * @param \Exception $e
+        * @param array $title_tag
+        * @param array $message_tag
+        * @param array $trace_tag
+        * @return string
+        */
        static function html(\Exception $e, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller'"]) {
                if ($e instanceof \http\Controller\Exception) {
                        $code = $e->getCode() ?: 500;
                        foreach ($e->getHeaders() as $key => $val) {
-                               HTTP::sendResponseHeader($key, $val);
+                               HTTP::setResponseHeader($key, $val);
                        }
                } else {
                        $code = 500;
@@ -44,4 +55,4 @@ class ExceptionHandler
                }
                return $html;
        }
-}
\ No newline at end of file
+}