X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=mdref%2FExceptionHandler.php;h=b641629f3c9372a63697e29ae2c8ee738ae5af1f;hb=80ea76521e2ccbbfac1ac0f33f91af1739ead561;hp=aeff40bd78275707a895de14d4bde164fc78ce7e;hpb=fc6b583627d21a26de8268df800fd44fbb9ac30d;p=mdref%2Fmdref diff --git a/mdref/ExceptionHandler.php b/mdref/ExceptionHandler.php index aeff40b..b641629 100644 --- a/mdref/ExceptionHandler.php +++ b/mdref/ExceptionHandler.php @@ -9,25 +9,40 @@ use http\Env as HTTP; */ class ExceptionHandler { + /** + * @var \http\Env\Response + */ + private $response; + /** * Set up error/exception/shutdown handler */ - public function __construct() { + public function __construct(\http\Env\Response $r) { + $this->response = $r; set_exception_handler($this); set_error_handler($this); register_shutdown_function($this); } - + + private static function cleanBuffers() { + while (ob_get_level()) { + if (!@ob_end_clean()) { + break; + } + } + } + /** * The exception/error/shutdown handler callback */ public function __invoke($e = null, $msg = null) { - if ($e instanceof \Exception) { + if ($e instanceof \Exception || $e instanceof \Throwable) { try { + self::cleanBuffers(); echo static::htmlException($e); } catch (\Exception $ignore) { headers_sent() or HTTP::setResponseCode(500); - die("FATAL ERROR"); + die("FATAL ERROR:\n$e\n$ignore"); } } elseif (isset($e, $msg)) { throw new \Exception($msg, $e); @@ -38,33 +53,26 @@ class ExceptionHandler case E_USER_ERROR: case E_CORE_ERROR: case E_COMPILE_ERROR: - while (ob_get_level()) { - if (!@ob_end_clean()) { - break; - } - } - $message = sprintf("%s in %s at line %d", + self::cleanBuffers(); + $message = sprintf("%s in %s at line %d", $error["message"], $error["file"], $error["line"]); echo static::htmlError("Application Error", $message, 500, ""); break; } } } - + /** * Format an exception as HTML and send appropriate exception info as HTTP headers - * @param \Exception $e + * @param Throwable $e * @param array $title_tag * @param array $message_tag * @param array $trace_tag * @return string */ - public static function htmlException(\Exception $e, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) { + public static function htmlException(/*\Throwable*/ $e, array $title_tag = ["h1"], array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) { if ($e instanceof Exception) { $code = $e->getCode() ?: 500; - foreach ($e->getHeaders() as $key => $val) { - HTTP::setResponseHeader($key, $val); - } } else { $code = 500; }