]> git.andy128k.dev Git - ipf.git/commitdiff
Custom Error Page
authoravl <alex.litovchenko@gmail.com>
Tue, 16 Sep 2008 13:51:41 +0000 (16:51 +0300)
committeravl <alex.litovchenko@gmail.com>
Tue, 16 Sep 2008 13:51:41 +0000 (16:51 +0300)
ipf/http/response/notfound.php
ipf/http/response/servererror.php
ipf/http/response/servererrordebug.php
ipf/router.php

index 4abbd24130d1d5d2a964d5a47c2ea1cd7d67f762..f965ab2b9ea751651957479fac4aba1bf38151d1 100644 (file)
@@ -4,6 +4,15 @@ class IPF_HTTP_Response_NotFound extends IPF_HTTP_Response
 {
     function __construct($content='404 Not Found', $mimetype=null)
     {
+        try{
+            $context = array(
+                'title'=>'404 Not Found',
+                'query_string'=>$_SERVER['QUERY_STRING'],
+                'MEDIA_URL'=>IPF::get('media_url'),
+                'ADMIN_MEDIA_URL'=>IPF::get('admin_media_url'),
+            );
+            $content = IPF_Shortcuts::RenderToString('404.html', $context);
+        }catch(IPF_Exception $e){}
         parent::__construct($content, $mimetype);
         $this->status_code = 404;
     }
index 7eeea86f793e470eee24288430d6abf740a55ce6..80a57a4b82c7afcde179d06d6696e6bf4fe81826 100644 (file)
@@ -2,9 +2,9 @@
 
 class IPF_HTTP_Response_ServerError extends IPF_HTTP_Response
 {
-    function __construct($content='Server Error', $mimetype=null)
+    function __construct($e, $mimetype=null)
     {
-        parent::__construct($content, $mimetype);
+        parent::__construct('<h1>500 Server Error</h1><p>Our apologies…</p><p>Please return later</p>', $mimetype);
         $this->status_code = 500;
     }
 }
index f300c7307be13f6a693b18fefcadc99814313692..04986f2c5597b7606a25d773509a7dbb51b647bf 100644 (file)
@@ -9,8 +9,6 @@ class IPF_HTTP_Response_ServerErrorDebug extends IPF_HTTP_Response
     }
 }
 
-
-
 function IPF_HTTP_Response_ServerErrorDebug_Pretty($e) 
 {
     $o = create_function('$in','return htmlspecialchars($in);');
index a8c7a4ff2f4dbb462ff969abcb6822762723f362..5bb0c5a1bd1de5975968eb604d5d5d2d098b4561 100644 (file)
@@ -2,42 +2,54 @@
 
 class IPF_Router
 {
+    public static function response500($e){
+        if (IPF::get('debug'))
+            return new IPF_HTTP_Response_ServerErrorDebug($e);
+        else
+            return new IPF_HTTP_Response_ServerError($e);
+    }
+    
     public static function dispatch($query='')
     {
-               $query = preg_replace('#^(/)+#', '/', '/'.$query);
-        $req = new IPF_HTTP_Request($query);
-        $middleware = array();
-        foreach (IPF::get('middlewares', array()) as $mw) {
-            $middleware[] = new $mw();
-        }
-        $skip = false;
-        foreach ($middleware as $mw) {
-            if (method_exists($mw, 'processRequest')) {
-                $response = $mw->processRequest($req);
-                if ($response !== false) {
-                    // $response is a response
-                    $response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
-                    $skip = true;
-                    break;
-                }    
-            }
-        }
-        if ($skip === false) {   
-            $response = IPF_Router::match($req);
-            if (!empty($req->response_vary_on)) {
-                $response->headers['Vary'] = $req->response_vary_on;
+        try{
+               $query = preg_replace('#^(/)+#', '/', '/'.$query);
+            $req = new IPF_HTTP_Request($query);
+            $middleware = array();
+            foreach (IPF::get('middlewares', array()) as $mw) {
+                $middleware[] = new $mw();
             }
-            $middleware = array_reverse($middleware);
+            $skip = false;
             foreach ($middleware as $mw) {
-                if (method_exists($mw, 'processResponse')) {
-                    $response = $mw->processResponse($req, $response);
-                }    
+                if (method_exists($mw, 'processRequest')) {
+                    $response = $mw->processRequest($req);
+                    if ($response !== false) {
+                        // $response is a response
+                        $response->render($req->method != 'HEAD' and !defined('IN_UNIT_TESTS'));
+                        $skip = true;
+                        break;
+                    }    
+                }
+            }
+            if ($skip === false) {   
+                $response = IPF_Router::match($req);
+                if (!empty($req->response_vary_on)) {
+                    $response->headers['Vary'] = $req->response_vary_on;
+                }
+                $middleware = array_reverse($middleware);
+                foreach ($middleware as $mw) {
+                    if (method_exists($mw, 'processResponse')) {
+                        $response = $mw->processResponse($req, $response);
+                    }    
+                }
+                //var_dump($response);
+                $response->render($req->method != 'HEAD');
             }
-            //var_dump($response);
-            $response->render($req->method != 'HEAD');
+            return array($req, $response);
+        } catch (IPF_Exception $e) {
+            $response = IPF_Router::response500($e);
+            $response->render();
         }
-        return array($req, $response);
-    }
+   }
 
     public static function match($req)
     {
@@ -45,34 +57,18 @@ class IPF_Router
             $prefix = $url['prefix'];
             foreach ($url['urls'] as $suburl){
                 $regex = $prefix.$suburl['regex'];
-                //print "Regex = $regex<br>";
-                //print "Query = {$req->query}<br>";
                 $match = array();
                 if (preg_match($regex, $req->query, $match)) {
                     try{
                         IPF::loadFunction($suburl['func']);
                         $r = $suburl['func']($req, $match);
-                        if (!is_a($r,'IPF_HTTP_Response'))
-                            return new IPF_HTTP_Response_ServerError('function '.$suburl['func'].'() must return IPF_HTTP_Response instance');
+                        if (!is_a($r,'IPF_HTTP_Response')){
+                            return IPF_Router::response500(new IPF_Exception('function '.$suburl['func'].'() must return IPF_HTTP_Response instance'));
+                        }
                         return $r;
                     } catch (IPF_Exception $e) {
-                        return new IPF_HTTP_Response_ServerErrorDebug($e);
-                    }
-                    /*
-                    try{
-                        IPF::loadFunction($suburl['func']);
-                        return $suburl['func']($req, $match);
-                    } catch (IPF_HTTP_Error404 $e) {
-                        // Need to add a 404 error handler
-                        // something like IPF::get('404_handler', 'class::method')
-                    } catch (Exception $e) {
-                        if (IPF::get('debug', false) == true) {
-                            return new IPF_HTTP_Response_ServerErrorDebug($e);
-                        } else {
-                            return new IPF_HTTP_Response_ServerError($e);
-                        }
+                        return IPF_Router::response500($e);
                     }
-                    */
                 }
             }
         }