Permalink
Browse files

Added error handling and Flash function

  • Loading branch information...
1 parent 5e55468 commit c04c06139f459d98ebe53fb08e3311b6797573c8 @lukemorton committed Dec 30, 2010
View
3 classes/controller.php
@@ -0,0 +1,3 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Controller extends Prophet_Controller {}
View
1 classes/controller/prophet.php
@@ -0,0 +1 @@
+<?php defined('SYSPATH') or die('No direct script access.');
View
38 classes/prophet.php
@@ -0,0 +1,38 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Prophet {
+
+ public function exception_handler(Exception $e)
+ {
+ if (Kohana::$environment === Kohana::DEVELOPMENT)
+ {
+ Kohana_Core::exception_handler($e);
+ }
+
+ // It's a nice time to log :)
+ Kohana::$log->add(Kohana::ERROR, Kohana::exception_text($e));
+
+ if ( ! defined('SUPPRESS_REQUEST'))
+ {
+ $request = array(
+ // Get code from current request
+ 'action' => Request::$current->code,
+
+ // If exception has a message this can be passed on
+ 'message' => $e->getMessage(),
+ );
+
+ // Override code if HTTP_Response_Exception thrown
+ if ($e instanceof HTTP_Response_Exception)
+ {
+ $request['action'] = $e->getCode();
+ }
+
+ echo Request::factory(Route::get('prophet_error')->uri($request))
+ ->execute()
+ ->send_headers()
+ ->response;
+ }
+ }
+
+}
View
6 classes/prophet/controller.php
@@ -33,8 +33,8 @@ class Prophet_Controller extends Kohana_Controller {
*/
public function before()
{
- // If not a viewless action
- if ( ! in_array($this->request->action, $this->viewless))
+ // If views not turned off and not a viewless action
+ if ($this->view !== FALSE && ! in_array($this->request->action, $this->viewless))
{
$view_parts = array();
@@ -63,7 +63,7 @@ public function before()
*/
public function after()
{
- if ($this->view !== NULL)
+ if ($this->view)
{
$this->request->response = $this->view;
}
View
26 classes/prophet/controller/prophet.php
@@ -0,0 +1,26 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Prophet_Controller_Prophet extends Controller {
+
+ public function before()
+ {
+ // External requests default to 500
+ if (Request::$instance === Request::$current)
+ {
+ $this->request->action = 500;
+ }
+
+ return parent::before();
+ }
+
+ public function action_404()
+ {
+ $this->request->code = 404;
+ }
+
+ public function action_500()
+ {
+ $this->request->code = 500;
+ }
+
+}
View
37 init.php
@@ -0,0 +1,37 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+if (Kohana::$errors)
+{
+ // Override Kohana exception handler
+ set_exception_handler(array('prophet', 'exception_handler'));
+}
+
+// Error Route for internal error requests
+Route::set('prophet_error', '<action>(/<message>)', array('action' => '[0-9]{3}', 'message' => '.*'))
+ ->defaults(array(
+ 'controller' => 'prophet'
+ ));
+
+// Catch All Route (404 Response)
+Route::set('prophet_catchall', '<catchall>', array('catchall' => '.*'))
+ ->defaults(array(
+ 'controller' => 'prophet',
+ 'action' => '404',
+ ));
+
+/**
+ * Helper function for set/get once data
+ *
+ * @param string Name of data
+ * @param mixed Data to be stored
+ * @return mixed Returns data if no value set
+ */
+function Flash($name, $value = NULL)
+{
+ if ($value === NULL)
+ {
+ return Session::instance()->get_once($name);
+ }
+
+ Session::instance()->set($name, $value);
+}

0 comments on commit c04c061

Please sign in to comment.