Permalink
Browse files

Adding a new exception renderer method to show useful error for faile…

…d queries
  • Loading branch information...
lorenzo committed Sep 4, 2011
1 parent 843f7e3 commit 3d0af8b690708f2f0f6ae48b2a797a181be168bf
@@ -59,6 +59,8 @@ class PagesController extends AppController {
* @return void * @return void
*/ */
public function display() { public function display() {
App::uses('ConnectionManager', 'Model');
ConnectionManager::getDatasource('default')->execute('dasd kdjskljklda dljsdljadlas dkjadjasldkas dlajdklajdla dajsdlajdlka');
$path = func_get_args(); $path = func_get_args();
$count = count($path); $count = count($path);
@@ -107,6 +107,10 @@ public function __construct(Exception $exception) {
if ($template == 'internalError') { if ($template == 'internalError') {
$template = 'error500'; $template = 'error500';
} }
} elseif ($exception instanceof PDOException) {
$method = 'pdoError';
$template = 'pdo_error';
$code = 500;
} elseif (!$methodExists) { } elseif (!$methodExists) {
$method = 'error500'; $method = 'error500';
if ($code >= 400 && $code < 500) { if ($code >= 400 && $code < 500) {
@@ -220,7 +224,7 @@ public function error400($error) {
*/ */
public function error500($error) { public function error500($error) {
$url = $this->controller->request->here(); $url = $this->controller->request->here();
$code = ($error->getCode() > 500) ? $error->getCode() : 500; $code = ($error->getCode() > 500 && $error->getCode() < 506) ? $error->getCode() : 500;
$this->controller->response->statusCode($code); $this->controller->response->statusCode($code);
$this->controller->set(array( $this->controller->set(array(
'name' => __d('cake', 'An Internal Error Has Occurred'), 'name' => __d('cake', 'An Internal Error Has Occurred'),
@@ -230,6 +234,29 @@ public function error500($error) {
$this->_outputMessage('error500'); $this->_outputMessage('error500');
} }
/**
* Convenience method to display a PDOException.
*
* @param PDOException $error
* @return void
*/
public function pdoError(PDOException $error) {
$url = $this->controller->request->here();
$code = 500;
$this->controller->response->statusCode($code);
$this->controller->set(array(
'code' => $code,
'url' => h($url),
'name' => $error->getMessage(),
'error' => $error,
));
try {
$this->_outputMessage($this->template);
} catch (Exception $e) {
$this->_outputMessageSafe('error500');
}
}
/** /**
* Generate the response using the controller object. * Generate the response using the controller object.
* *
@@ -430,18 +430,27 @@ protected function _execute($sql, $params = array(), $prepareOptions = array())
} }
} }
$query = $this->_connection->prepare($sql, $prepareOptions); try {
$query->setFetchMode(PDO::FETCH_LAZY); $query = $this->_connection->prepare($sql, $prepareOptions);
if (!$query->execute($params)) { $query->setFetchMode(PDO::FETCH_LAZY);
$this->_results = $query; if (!$query->execute($params)) {
$query->closeCursor(); $this->_results = $query;
return false; $query->closeCursor();
} return false;
if (!$query->columnCount()) { }
$query->closeCursor(); if (!$query->columnCount()) {
return true; $query->closeCursor();
return true;
}
return $query;
} catch (PDOException $e) {
if (isset($query->queryString)) {
$e->queryString = $query->queryString;
} else {
$e->queryString = $sql;
}
throw $e;
} }
return $query;
} }
/** /**
@@ -665,4 +665,27 @@ public function testRenderWithNoRequest() {
$this->assertContains('Internal Error', $result); $this->assertContains('Internal Error', $result);
} }
/**
* Tests the output of rendering a PDOException
*
* @return void
*/
public function testPDOException() {
$exception = new PDOException('There was an error in the SQL query');
$exception->queryString = 'SELECT * from poo_query < 5 and :seven';
$exception->params = array('seven' => 7);
$ExceptionRenderer = new ExceptionRenderer($exception);
$ExceptionRenderer->controller->response = $this->getMock('CakeResponse', array('statusCode', '_sendHeader'));
$ExceptionRenderer->controller->response->expects($this->once())->method('statusCode')->with(500);
ob_start();
$ExceptionRenderer->render();
$result = ob_get_clean();
$this->assertPattern('/<h2>Database Error<\/h2>/', $result);
$this->assertPattern('/There was an error in the SQL query/', $result);
$this->assertPattern('/SELECT \* from poo_query < 5 and :seven/', $result);
$this->assertPattern('/"seven" => 7/', $result);
}
} }
@@ -0,0 +1,38 @@
<?php
/**
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package 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 __d('cake_dev', 'Database Error'); ?></h2>
<p class="error">
<strong><?php echo __d('cake_dev', 'Error'); ?>: </strong>
<?php echo h($error->getMessage()); ?>
</p>
<?php if (!empty($error->queryString)) : ?>
<p class="notice">
<strong><?php echo __d('cake_dev', 'SQL Query'); ?>: </strong>
<?php echo $error->queryString; ?>
</p>
<?php endif; ?>
<?php if (!empty($error->params)) : ?>
<strong><?php echo __d('cake_dev', 'SQL Query Params'); ?>: </strong>
<?php echo Debugger::dump($error->params); ?>
<?php endif; ?>
<p class="notice">
<strong><?php echo __d('cake_dev', 'Notice'); ?>: </strong>
<?php echo __d('cake_dev', 'If you want to customize this error message, create %s', APP_DIR . DS . 'View' . DS . 'Errors' . DS . 'pdo_error.ctp'); ?>
</p>
<?php echo $this->element('exception_stack_trace'); ?>

0 comments on commit 3d0af8b

Please sign in to comment.