From: Andrey Kutejko Date: Sun, 11 Jan 2015 01:46:22 +0000 (+0200) Subject: catch errors and fatal errors X-Git-Tag: 0.6~73 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=ab32296d5c18909d9c0fd8e80297e9223b70b8b3;p=ipf.git catch errors and fatal errors --- diff --git a/ipf/error/500_debug.php b/ipf/error/500_debug.php index 85b5014..58cfc6e 100644 --- a/ipf/error/500_debug.php +++ b/ipf/error/500_debug.php @@ -112,19 +112,21 @@ class IPF_Server_Error_Page_Debug extends IPF_Error_Page 'args' => $args, ); - $file = $frame['file']; - $line = $frame['line']; + $file = @$frame['file']; + $line = @$frame['line']; } - $result[] = array( - 'file' => $file, - 'line' => $line, - 'class' => '', - 'type' => '', - 'function' => '', - 'func' => '', - 'args' => array(), - ); + if ($file) { + $result[] = array( + 'file' => $file, + 'line' => $line, + 'class' => '', + 'type' => '', + 'function' => '', + 'func' => '', + 'args' => array(), + ); + } return $result; } diff --git a/ipf/middleware/error.php b/ipf/middleware/error.php index ee009f8..28fb311 100644 --- a/ipf/middleware/error.php +++ b/ipf/middleware/error.php @@ -4,20 +4,37 @@ class IPF_Error_Middleware extends IPF_Middleware { function call($request) { + $previous = set_error_handler(array($this, 'error_handler')); + register_shutdown_function(array($this, 'shutdown_handler'), $request); try { - $response = null; - if ($this->next) { - $response = $this->next->call($request); - } + if (!$this->next) + throw new \Exception('Cannot process request. No next middleware is given.'); - if (!($response instanceof IPF_HTTP_Response)) { - throw new \Exception('Response is not a IPF_HTTP_Response'); - } + $response = $this->next->call($request); - return $response; + if (!($response instanceof IPF_HTTP_Response)) + throw new \Exception('Response is not a IPF_HTTP_Response'); } catch (\Exception $exception) { error_log($exception); - return new IPF_HTTP_Response_ServerError($request, $exception); + $response = new IPF_HTTP_Response_ServerError($request, $exception); + } + set_error_handler($previous); + return $response; + } + + function error_handler($errno, $errstr, $errfile, $errline) + { + if (error_reporting() & $errno) + throw new \ErrorException($errstr, 0, $errno, $errfile, $errline); + } + + function shutdown_handler($request) + { + $error = error_get_last(); + if ($error) { + $exception = new \ErrorException($error['message'], 0, $error['type'], $error['file'], $error['line']); + $response = new IPF_HTTP_Response_ServerError($request, $exception); + $response->render(); } } }