Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

exception handler added

  • Loading branch information...
commit 1d3d5165d3c0f9b2795dc13ba825c3626bb6423f 1 parent 7bd1690
authored
5  system/core/CodeIgniter.php
@@ -68,10 +68,11 @@
68 68
 
69 69
 /*
70 70
  * ------------------------------------------------------
71  
- *  Define a custom error handler so we can log PHP errors
  71
+ *  Define a custom error and exception handler so we can log PHP errors
72 72
  * ------------------------------------------------------
73 73
  */
74  
-	set_error_handler('_exception_handler');
  74
+	set_error_handler('_error_handler');
  75
+	set_exception_handler('_exception_handler');
75 76
 
76 77
 	if ( ! is_php('5.4'))
77 78
 	{
52  system/core/Common.php
@@ -501,12 +501,12 @@ function set_status_header($code = 200, $text = '')
501 501
 
502 502
 // --------------------------------------------------------------------
503 503
 
504  
-if ( ! function_exists('_exception_handler'))
  504
+if ( ! function_exists('_error_handler'))
505 505
 {
506 506
 	/**
507  
-	 * Exception Handler
  507
+	 * Error Handler
508 508
 	 *
509  
-	 * This is the custom exception handler that is declaired at the top
  509
+	 * This is the custom error handler that is declaired at the top
510 510
 	 * of Codeigniter.php. The main reason we use this is to permit
511 511
 	 * PHP errors to be logged in our own log files since the user may
512 512
 	 * not have access to server logs. Since this function
@@ -520,7 +520,7 @@ function set_status_header($code = 200, $text = '')
520 520
 	 * @param	int
521 521
 	 * @return	void
522 522
 	 */
523  
-	function _exception_handler($severity, $message, $filepath, $line)
  523
+	function _error_handler($severity, $message, $filepath, $line)
524 524
 	{
525 525
 		$_error =& load_class('Exceptions', 'core');
526 526
 
@@ -544,6 +544,50 @@ function _exception_handler($severity, $message, $filepath, $line)
544 544
 
545 545
 // --------------------------------------------------------------------
546 546
 
  547
+if ( ! function_exists('_exception_handler'))
  548
+{
  549
+	/**
  550
+    * Default Exception Handler
  551
+    * Triggers on uncaught exceptions
  552
+    *
  553
+    * @access   private
  554
+    * @return   void
  555
+    */
  556
+    function _exception_handler($exception)
  557
+    {
  558
+        $heading = $exception->getHeading();
  559
+        $message = $exception->getMessage();
  560
+
  561
+        parent::log_exception(E_ERROR, $message, $exception->getFile(), $exception->getLine());
  562
+
  563
+        set_status_header($exception->getStatus());
  564
+
  565
+		$_input =& load_class('Input');
  566
+		
  567
+        if ($_input->is_ajax_request())
  568
+        {
  569
+            echo $message;
  570
+        }
  571
+        else
  572
+        {
  573
+            if (ob_get_level() > $this->ob_level + 1)
  574
+            {
  575
+                ob_end_flush();
  576
+            }
  577
+            ob_start();
  578
+            include(APPPATH.'errors/'.$exception->getTemplate().'.php');
  579
+            $buffer = ob_get_contents();
  580
+            ob_end_clean();
  581
+
  582
+            echo $buffer;
  583
+        }
  584
+		
  585
+        exit;
  586
+    }
  587
+}
  588
+
  589
+// --------------------------------------------------------------------
  590
+
547 591
 if ( ! function_exists('remove_invisible_characters'))
548 592
 {
549 593
 	/**
42  system/core/Exceptions.php
@@ -132,23 +132,15 @@ public function show_404($page = '', $log_error = TRUE)
132 132
 	 * @param 	int	the status code
133 133
 	 * @return	string
134 134
 	 */
135  
-	public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
136  
-	{
137  
-		set_status_header($status_code);
138  
-
139  
-		$message = '<p>'.implode('</p><p>', is_array($message) ? $message : array($message)).'</p>';
  135
+    public function show_error($heading, $message, $template = 'error_general', $status_code = 500)
  136
+    {
  137
+        set_status_header($status_code);
140 138
 
141  
-		if (ob_get_level() > $this->ob_level + 1)
142  
-		{
143  
-			ob_end_flush();
144  
-		}
145  
-		ob_start();
146  
-		include(VIEWPATH.'errors/'.$template.'.php');
147  
-		$buffer = ob_get_contents();
148  
-		ob_end_clean();
149  
-		return $buffer;
150  
-	}
  139
+        $message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
151 140
 
  141
+        throw new CI_RuntimeException($heading, $message, $template, $status_code, E_ERROR);
  142
+    }
  143
+	
152 144
 	// --------------------------------------------------------------------
153 145
 
154 146
 	/**
@@ -185,5 +177,25 @@ public function show_php_error($severity, $message, $filepath, $line)
185 177
 
186 178
 }
187 179
 
  180
+class CI_RuntimeException extends Exception {
  181
+
  182
+    protected $heading;
  183
+    protected $template;
  184
+    protected $status_code;
  185
+
  186
+    public function __construct($heading, $message, $template = 'error_general', $status_code = 500, $code = 0, Exception $previous = null)
  187
+    {
  188
+        parent::__construct($message, $code, $previous);
  189
+
  190
+        $this->heading      = $heading;
  191
+        $this->template     = $template;
  192
+        $this->status_code  = $status_code;
  193
+    }
  194
+
  195
+    final public function getHeading(){ return $this->heading; }
  196
+    final public function getTemplate(){ return $this->template; }
  197
+    final public function getStatus(){ return $this->status_code; }
  198
+}
  199
+
188 200
 /* End of file Exceptions.php */
189 201
 /* Location: ./system/core/Exceptions.php */
2  system/libraries/Driver.php
@@ -240,7 +240,7 @@ public function __call($method, $args = array())
240 240
 		}
241 241
 
242 242
 		$trace = debug_backtrace();
243  
-		_exception_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
  243
+		_error_handler(E_ERROR, "No such method '{$method}'", $trace[1]['file'], $trace[1]['line']);
244 244
 		exit;
245 245
 	}
246 246
 

0 notes on commit 1d3d516

Please sign in to comment.
Something went wrong with that request. Please try again.