Permalink
Browse files

exception handler added

  • Loading branch information...
1 parent 7bd1690 commit 1d3d5165d3c0f9b2795dc13ba825c3626bb6423f darvanitis committed Oct 3, 2012
Showing with 79 additions and 22 deletions.
  1. +3 −2 system/core/CodeIgniter.php
  2. +48 −4 system/core/Common.php
  3. +27 −15 system/core/Exceptions.php
  4. +1 −1 system/libraries/Driver.php
@@ -68,10 +68,11 @@
/*
* ------------------------------------------------------
- * Define a custom error handler so we can log PHP errors
+ * Define a custom error and exception handler so we can log PHP errors
* ------------------------------------------------------
*/
- set_error_handler('_exception_handler');
+ set_error_handler('_error_handler');
+ set_exception_handler('_exception_handler');
if ( ! is_php('5.4'))
{
View
@@ -501,12 +501,12 @@ function set_status_header($code = 200, $text = '')
// --------------------------------------------------------------------
-if ( ! function_exists('_exception_handler'))
+if ( ! function_exists('_error_handler'))
{
/**
- * Exception Handler
+ * Error Handler
*
- * This is the custom exception handler that is declaired at the top
+ * This is the custom error handler that is declaired at the top
* of Codeigniter.php. The main reason we use this is to permit
* PHP errors to be logged in our own log files since the user may
* not have access to server logs. Since this function
@@ -520,7 +520,7 @@ function set_status_header($code = 200, $text = '')
* @param int
* @return void
*/
- function _exception_handler($severity, $message, $filepath, $line)
+ function _error_handler($severity, $message, $filepath, $line)
{
$_error =& load_class('Exceptions', 'core');
@@ -544,6 +544,50 @@ function _exception_handler($severity, $message, $filepath, $line)
// --------------------------------------------------------------------
+if ( ! function_exists('_exception_handler'))
+{
+ /**
+ * Default Exception Handler
+ * Triggers on uncaught exceptions
+ *
+ * @access private
+ * @return void
+ */
+ function _exception_handler($exception)
+ {
+ $heading = $exception->getHeading();
+ $message = $exception->getMessage();
+
+ parent::log_exception(E_ERROR, $message, $exception->getFile(), $exception->getLine());
+
+ set_status_header($exception->getStatus());
+
+ $_input =& load_class('Input');
+
+ if ($_input->is_ajax_request())
+ {
+ echo $message;
+ }
+ else
+ {
+ if (ob_get_level() > $this->ob_level + 1)
+ {
+ ob_end_flush();
+ }
+ ob_start();
+ include(APPPATH.'errors/'.$exception->getTemplate().'.php');
+ $buffer = ob_get_contents();
+ ob_end_clean();
+
+ echo $buffer;
+ }
+
+ exit;
+ }
+}
+
+// --------------------------------------------------------------------
+
if ( ! function_exists('remove_invisible_characters'))
{
/**
View
@@ -132,23 +132,15 @@ public function show_404($page = '', $log_error = TRUE)
* @param int the status code
* @return string
*/
- public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
- {
- set_status_header($status_code);
-
- $message = '<p>'.implode('</p><p>', is_array($message) ? $message : array($message)).'</p>';
+ public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
+ {
+ set_status_header($status_code);
- if (ob_get_level() > $this->ob_level + 1)
- {
- ob_end_flush();
- }
- ob_start();
- include(VIEWPATH.'errors/'.$template.'.php');
- $buffer = ob_get_contents();
- ob_end_clean();
- return $buffer;
- }
+ $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
+ throw new CI_RuntimeException($heading, $message, $template, $status_code, E_ERROR);
+ }
+
// --------------------------------------------------------------------
/**
@@ -185,5 +177,25 @@ public function show_php_error($severity, $message, $filepath, $line)
}
+class CI_RuntimeException extends Exception {
+
+ protected $heading;
+ protected $template;
+ protected $status_code;
+
+ public function __construct($heading, $message, $template = 'error_general', $status_code = 500, $code = 0, Exception $previous = null)
+ {
+ parent::__construct($message, $code, $previous);
+
+ $this->heading = $heading;
+ $this->template = $template;
+ $this->status_code = $status_code;
+ }
+
+ final public function getHeading(){ return $this->heading; }
+ final public function getTemplate(){ return $this->template; }
+ final public function getStatus(){ return $this->status_code; }
+}
+
/* End of file Exceptions.php */
/* Location: ./system/core/Exceptions.php */
@@ -240,7 +240,7 @@ public function __call($method, $args = array())
}
$trace = debug_backtrace();
- _exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
+ _error_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
exit;
}

0 comments on commit 1d3d516

Please sign in to comment.