Skip to content
This repository
Browse code

Added a handler for fatal errors, showing internal server error page …

…when debug is disabled or a custom fatal error page when enabled.
  • Loading branch information...
commit 440f0c38eb4ee8ce7c20b8a38325e84a5a2311ca 1 parent f677bfe
Juan Basso authored March 29, 2012
5  app/Config/core.php
@@ -45,13 +45,16 @@
45 45
  *    including anonymous functions.
46 46
  * - `level` - int - The level of errors you are interested in capturing.
47 47
  * - `trace` - boolean - Include stack traces for errors in log files.
  48
+ * - `handleFatalError` - boolean - Enable the CakePHP fatal error handler, generating custom
  49
+ *    pages for fatal errors instead of show broke pages.
48 50
  *
49 51
  * @see ErrorHandler for more information on error handling and configuration.
50 52
  */
51 53
 	Configure::write('Error', array(
52 54
 		'handler' => 'ErrorHandler::handleError',
53 55
 		'level' => E_ALL & ~E_DEPRECATED,
54  
-		'trace' => true
  56
+		'trace' => true,
  57
+		'handleFatalError' => true
55 58
 	));
56 59
 
57 60
 /**
1  lib/Cake/Core/Configure.php
@@ -345,5 +345,6 @@ protected static function _setErrorHandlers($error, $exception) {
345 345
 		if (!empty($exception['handler'])) {
346 346
 			set_exception_handler($exception['handler']);
347 347
 		}
  348
+		register_shutdown_function('ErrorHandler::handleFatalError');
348 349
 	}
349 350
 }
38  lib/Cake/Error/ErrorHandler.php
@@ -184,6 +184,44 @@ public static function handleError($code, $description, $file = null, $line = nu
184 184
 	}
185 185
 
186 186
 /**
  187
+ * Generate an error page when some fatal error happens.
  188
+ *
  189
+ * Use Configure::write('Error.handleFatalError', false) to disable this feature
  190
+ *
  191
+ * @return void
  192
+ */
  193
+	public static function handleFatalError() {
  194
+		if (Configure::read('Error.handleFatalError') !== true) {
  195
+			return;
  196
+		}
  197
+
  198
+		$lastError = error_get_last();
  199
+		if (!is_array($lastError)) {
  200
+			return;
  201
+		}
  202
+
  203
+		list($error, $log) = self::mapErrorCode($lastError['type']);
  204
+		if ($log !== LOG_ERROR) {
  205
+			return;
  206
+		}
  207
+
  208
+		$logMessage = $error . ' (' . $lastError['type'] . '): ' . $lastError['message'] . ' in [' . $lastError['file'] . ', line ' . $lastError['line'] . ']';
  209
+		CakeLog::write($log, $logMessage);
  210
+
  211
+		$exceptionHandler = Configure::read('Exception.handler');
  212
+		if (!is_callable($exceptionHandler)) {
  213
+			return;
  214
+		}
  215
+
  216
+		ob_clean();
  217
+		if (Configure::read('debug')) {
  218
+			call_user_func($exceptionHandler, new FatalErrorException($lastError['message'], 500, $lastError['file'], $lastError['line']));
  219
+		} else {
  220
+			call_user_func($exceptionHandler, new InternalErrorException());
  221
+		}
  222
+	}
  223
+
  224
+/**
187 225
  * Map an error code into an Error word, and log location.
188 226
  *
189 227
  * @param integer $code Error code to map
27  lib/Cake/Error/exceptions.php
@@ -517,3 +517,30 @@ class XmlException extends CakeException {
517 517
  */
518 518
 class ConsoleException extends CakeException {
519 519
 }
  520
+
  521
+/**
  522
+ * Represents a fatal error
  523
+ *
  524
+ * @package       Cake.Error
  525
+ */
  526
+class FatalErrorException extends CakeException {
  527
+
  528
+/**
  529
+ * Constructor
  530
+ *
  531
+ * @param string $message
  532
+ * @param integer $code
  533
+ * @param string $file
  534
+ * @param integer $line
  535
+ */
  536
+	public function __construct($message, $code = 500, $file = null, $line = null) {
  537
+		parent::__construct($message, $code);
  538
+		if ($file) {
  539
+			$this->file = $file;
  540
+		}
  541
+		if ($line) {
  542
+			$this->line = $line;
  543
+		}
  544
+	}
  545
+
  546
+}
35  lib/Cake/View/Errors/fatal_error.ctp
... ...
@@ -0,0 +1,35 @@
  1
+<?php
  2
+/**
  3
+ *
  4
+ * PHP 5
  5
+ *
  6
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  7
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  8
+ *
  9
+ * Licensed under The MIT License
  10
+ * Redistributions of files must retain the above copyright notice.
  11
+ *
  12
+ * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  13
+ * @link          http://cakephp.org CakePHP(tm) Project
  14
+ * @package       Cake.View.Errors
  15
+ * @since         CakePHP(tm) v 2.2.0
  16
+ * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
  17
+ */
  18
+?>
  19
+<h2><?php echo __d('cake_dev', 'Fatal Error'); ?></h2>
  20
+<p class="error">
  21
+	<strong><?php echo __d('cake_dev', 'Error'); ?>: </strong>
  22
+	<?php echo h($error->getMessage()); ?>
  23
+	<br>
  24
+
  25
+	<strong><?php echo __d('cake_dev', 'File'); ?>: </strong>
  26
+	<?php echo h($error->getFile()); ?>
  27
+	<br>
  28
+
  29
+	<strong><?php echo __d('cake_dev', 'Line'); ?>: </strong>
  30
+	<?php echo h($error->getLine()); ?>
  31
+</p>
  32
+<p class="notice">
  33
+	<strong><?php echo __d('cake_dev', 'Notice'); ?>: </strong>
  34
+	<?php echo __d('cake_dev', 'If you want to customize this error message, create %s', APP_DIR . DS . 'View' . DS . 'Errors' . DS . 'fatal_error.ctp'); ?>
  35
+</p>

0 notes on commit 440f0c3

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