Commits

Michael Haschke committed 2ef3ee7

[add] insert methods to die by throwing an exception error, or to die by returning an http error code

Comments (0)

Files changed (1)

foafpressapp/core/Foafpress.php

                 return;
         }
 
-        // TODO: send 404 if no file/resource is available
-        die($file.' is not found!');
+        $this->dieWithHttpErrorCode($this->URI_Request.' is not found', 404);
         
         return;
     }
         }
         else
         {
-            // TODO: throw exception
-            die('Foafpress'.$template_type.'.php not found!');
+            // TODO: line?
+            $this->dieWithException('Foafpress'.$template_type.'.php not found!');
         }
 
         // use ns:concept to set template and controller
             }
             else
             {
-                // TODO: throw exception
-                die($FP->ns_prefix.DIRECTORY_SEPARATOR.$concept.$template_type.'.php not found!');
+                // TODO: line?
+                $this->dieWithException($FP->ns_prefix.DIRECTORY_SEPARATOR.$concept.$template_type.'.php not found!');
             }
             
             // try to set controller
                 
                 if (!isset($_SERVER['REQUEST_METHOD']) || !$_SERVER['REQUEST_METHOD'])
                 {
-                    throw new Exception('Empty request method!'); // TODO http error code
+                    $this->dieWithHttpErrorCode('Empty request method!', 503); // TODO is 503 right?
                 }
                 elseif (in_array($_SERVER['REQUEST_METHOD'], $this->config['supportedmethods']))
                 {
                 }
                 else
                 {
-                    throw new Exception($_SERVER['REQUEST_METHOD'].' is not supported here!'); // TODO http error code
+                    $this->dieWithHttpErrorCode($_SERVER['REQUEST_METHOD'].' is not supported here!', 503); // TODO is 503 right?
                 }
             }
             catch(Exception $e)
 
     }
 
+    public function dieWithHttpErrorCode($message = null, $http_error_code = 0)
+    {
+        if (!$message || !$http_error_code)
+        {
+            throw new Exception('Need status message and HTTP error code! Message: '.$message.' / Code: '.$http_error_code);
+        }
+
+        header("HTTP/1.0 ".$http_error_code, true, $http_error_code);
+        die('<h1>'.$message.'</h1>');
+
+    }
+
+    public function dieWithException($message = null)
+    {
+        if (!$message)
+        {
+            throw new Exception('Need error message! Message: '.$message);
+        }
+
+        throw new Exception($message); // TODO insert line where the exception was triggered
+
+    }
+
     public function addLogMessage($msg)
     {
         $msg = $msg.' -- memory '.intval(memory_get_usage(true)/1024).'kb';