6 use function debug_print_backtrace
;
7 use function error_get_last
;
8 use function headers_sent
;
10 use function ob_end_clean
;
11 use function ob_get_clean
;
12 use function ob_get_level
;
13 use function ob_start
;
14 use function register_shutdown_function
;
15 use function set_error_handler
;
16 use function set_exception_handler
;
18 use const E_COMPILE_ERROR
;
19 use const E_CORE_ERROR
;
22 use const E_USER_ERROR
;
25 * Exception and error handler
27 class ExceptionHandler
30 * @var \http\Env\Response
35 * Set up error/exception/shutdown handler
36 * @param Env\Response $r
38 public function __construct(Env\Response
$r) {
40 set_exception_handler($this);
41 set_error_handler($this);
42 register_shutdown_function($this);
46 * Clean output buffers
48 private static function cleanBuffers() : void
{
49 while (ob_get_level()) {
50 if (!@ob_end_clean
()) {
57 * The exception/error/shutdown handler callback
59 * @param \Throwable|string $e
63 public function __invoke($e = null, ?
string $msg = null) : void
{
64 if ($e instanceof \Throwable
) {
67 echo static::htmlException($e);
68 } catch (\Exception
$ignore) {
69 headers_sent() or Env
::setResponseCode(500);
70 die("FATAL ERROR:\n$e\n$ignore");
72 } elseif (isset($e, $msg)) {
73 throw new \
Exception($msg, $e);
74 } elseif (($error = error_get_last())) {
75 switch ($error["type"]) {
82 $message = sprintf("%s in %s at line %d",
83 $error["message"], $error["file"], $error["line"]);
84 echo static::htmlError("Application Error", $message, 500, "");
91 * Format an exception as HTML and send appropriate exception info as HTTP headers
92 * @param \Throwable $e
93 * @param array $title_tag
94 * @param array $message_tag
95 * @param array $trace_tag
98 public static function htmlException(\Throwable
$e, array $title_tag = ["h1"], array $message_tag = ["p"],
99 array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) : string {
100 if ($e instanceof Exception
) {
101 $code = $e->getCode() ?
: 500;
106 for ($html = ""; $e; $e = $e->getPrevious()) {
107 $html .= static::htmlError(Env
::getResponseStatusForCode($code),
108 $e->getMessage(), $code, $e->getTraceAsString(),
109 $title_tag, $message_tag, $trace_tag);
115 * Format an error as HTML
116 * @param string $title
117 * @param string $message
119 * @param string $trace
120 * @param array $title_tag
121 * @param array $message_tag
122 * @param array $trace_tag
125 public static function htmlError($title, $message, $code, $trace = null, array $title_tag = ["h1"],
126 array $message_tag = ["p"], array $trace_tag = ["pre", "style='font-size:smaller;overflow-x:scroll'"]) : string {
127 Env
::setResponseCode($code);
129 $html = sprintf("<%s>%s</%s>\n<%s>%s</%s>\n",
130 implode(" ", $title_tag), $title, $title_tag[0],
131 implode(" ", $message_tag), $message, $message_tag[0]);
133 if (!isset($trace)) {
135 debug_print_backtrace();
136 $trace = ob_get_clean();
138 if (!empty($trace)) {
139 $html .= sprintf("<%s>%s</%s>\n",
140 implode(" ", $trace_tag), $trace, $trace_tag[0]);