]> git.andy128k.dev Git - ipf.git/commitdiff
catch errors and fatal errors
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 11 Jan 2015 01:46:22 +0000 (03:46 +0200)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 11 Jan 2015 01:50:53 +0000 (03:50 +0200)
ipf/error/500_debug.php
ipf/middleware/error.php

index 85b50144656934d7dec2054a17326000cbf23de6..58cfc6ea3bc12c71d1026eb632b3ddb81e512241 100644 (file)
@@ -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;
     }
index ee009f8a7ce8fdbc8137778f5e693221587788a0..28fb3117c8e8142d79d89ed73d8c5765a653619b 100644 (file)
@@ -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();
         }
     }
 }