Permalink
Browse files

Changing error handler to send a HTTP/1.1 500 error when missingTable…

…, missingDatabase or missingConnection is encountered. Increased test coverage of libs/error.php to 100%. Fixes #70.
  • Loading branch information...
1 parent 96c245c commit 7b1ccfff57a446d12a8ca2b9dff7e0c3780a3681 @jperras jperras committed Dec 13, 2009
Showing with 79 additions and 3 deletions.
  1. +29 −2 cake/libs/error.php
  2. +24 −0 cake/libs/view/errors/error500.ctp
  3. +26 −1 cake/tests/cases/libs/error.test.php
View
31 cake/libs/error.php
@@ -92,7 +92,6 @@ function __construct($method, $messages) {
$this->controller =& new Controller();
$this->controller->viewPath = 'errors';
}
-
$options = array('escape' => false);
$messages = Sanitize::clean($messages, $options);
@@ -156,7 +155,7 @@ function error404($params) {
$url = $this->controller->here;
}
$url = Router::normalize($url);
- header("HTTP/1.0 404 Not Found");
+ $this->controller->header("HTTP/1.0 404 Not Found");
$this->controller->set(array(
'code' => '404',
'name' => __('Not Found', true),
@@ -167,6 +166,28 @@ function error404($params) {
}
/**
+ * Convenience method to display a 500 page.
+ *
+ * @param array $params Parameters for controller
+ * @access public
+ */
+ function error500($params) {
+ extract($params, EXTR_OVERWRITE);
+
+ if (!isset($url)) {
+ $url = $this->controller->here;
+ }
+ $url = Router::normalize($url);
+ $this->controller->header("HTTP/1.0 500 Not Found");
+ $this->controller->set(array(
+ 'code' => '500',
+ 'name' => __('An Internal Error Has Occurred', true),
+ 'message' => h($url),
+ 'base' => $this->controller->base
+ ));
+ $this->_outputMessage('error500');
+ }
+/**
* Renders the Missing Controller web page.
*
* @param array $params Parameters for controller
@@ -229,7 +250,9 @@ function privateAction($params) {
function missingTable($params) {
extract($params, EXTR_OVERWRITE);
+ $this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
+ 'code' => '500',
'model' => $className,
'table' => $table,
'title' => __('Missing Database Table', true)
@@ -244,7 +267,9 @@ function missingTable($params) {
* @access public
*/
function missingDatabase($params = array()) {
+ $this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
+ 'code' => '500',
'title' => __('Scaffold Missing Database Connection', true)
));
$this->_outputMessage('missingScaffolddb');
@@ -294,7 +319,9 @@ function missingLayout($params) {
function missingConnection($params) {
extract($params, EXTR_OVERWRITE);
+ $this->controller->header("HTTP/1.0 500 Internal Server Error");
$this->controller->set(array(
+ 'code' => '500',
'model' => $className,
'title' => __('Missing Database Connection', true)
));
View
24 cake/libs/view/errors/error500.ctp
@@ -0,0 +1,24 @@
+<?php
+/**
+ *
+ * PHP versions 4 and 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @package cake
+ * @subpackage cake.cake.libs.view.templates.errors
+ * @since CakePHP(tm) v 0.10.0.1076
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+?>
+<h2><?php echo $name; ?></h2>
+<p class="error">
+ <strong><?php __('Error'); ?>: </strong>
+ <?php echo sprintf(__('An Internal Error Has Occurred.', true), "<strong>'{$message}'</strong>")?>
+</p>
View
27 cake/tests/cases/libs/error.test.php
@@ -288,7 +288,7 @@ function testSubclassMethodsNotBeingConvertedToError() {
$ErrorHandler = new MyCustomErrorHandler('missingWidgetThing', array('message' => 'doh!'));
$result = ob_get_clean();
$this->assertEqual($result, 'widget thing is missing', 'Method declared in subclass converted to error404. %s');
-
+
Configure::write('debug', 0);
ob_start();
$ErrorHandler = new MyCustomErrorHandler('missingController', array('message' => 'Page not found'));
@@ -346,6 +346,29 @@ function testError404() {
}
/**
+ * testError500 method
+ *
+ * @access public
+ * @return void
+ */
+ function testError500() {
+ ob_start();
+ $TestErrorHandler = new TestErrorHandler('error500', array(
+ 'message' => 'An Internal Error Has Occurred'
+ ));
+ $result = ob_get_clean();
+ $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
+
+ ob_start();
+ $TestErrorHandler = new TestErrorHandler('error500', array(
+ 'message' => 'An Internal Error Has Occurred',
+ 'code' => '500'
+ ));
+ $result = ob_get_clean();
+ $this->assertPattern('/<h2>An Internal Error Has Occurred<\/h2>/', $result);
+ }
+
+/**
* testMissingController method
*
* @access public
@@ -408,6 +431,7 @@ function testMissingTable() {
ob_start();
$TestErrorHandler = new TestErrorHandler('missingTable', array('className' => 'Article', 'table' => 'articles'));
$result = ob_get_clean();
+ $this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
$this->assertPattern('/<h2>Missing Database Table<\/h2>/', $result);
$this->assertPattern('/table <em>articles<\/em> for model <em>Article<\/em>/', $result);
}
@@ -422,6 +446,7 @@ function testMissingDatabase() {
ob_start();
$TestErrorHandler = new TestErrorHandler('missingDatabase', array());
$result = ob_get_clean();
+ $this->assertPattern('/HTTP\/1\.0 500 Internal Server Error/', $result);
$this->assertPattern('/<h2>Missing Database Connection<\/h2>/', $result);
$this->assertPattern('/Confirm you have created the file/', $result);
}

0 comments on commit 7b1ccff

Please sign in to comment.