diff --git a/lib/Cake/Controller/PagesController.php b/lib/Cake/Controller/PagesController.php index bd6a3e7d7..91fadb182 100644 --- a/lib/Cake/Controller/PagesController.php +++ b/lib/Cake/Controller/PagesController.php @@ -59,6 +59,8 @@ class PagesController extends AppController { * @return void */ public function display() { + App::uses('ConnectionManager', 'Model'); + ConnectionManager::getDatasource('default')->execute('dasd kdjskljklda dljsdljadlas dkjadjasldkas dlajdklajdla dajsdlajdlka'); $path = func_get_args(); $count = count($path); diff --git a/lib/Cake/Error/ExceptionRenderer.php b/lib/Cake/Error/ExceptionRenderer.php index d35142375..357cb9ad1 100644 --- a/lib/Cake/Error/ExceptionRenderer.php +++ b/lib/Cake/Error/ExceptionRenderer.php @@ -107,6 +107,10 @@ class ExceptionRenderer { if ($template == 'internalError') { $template = 'error500'; } + } elseif ($exception instanceof PDOException) { + $method = 'pdoError'; + $template = 'pdo_error'; + $code = 500; } elseif (!$methodExists) { $method = 'error500'; if ($code >= 400 && $code < 500) { @@ -220,7 +224,7 @@ class ExceptionRenderer { */ public function error500($error) { $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->set(array( 'name' => __d('cake', 'An Internal Error Has Occurred'), @@ -230,6 +234,29 @@ class ExceptionRenderer { $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. * diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 637cd1e45..add88cd98 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/lib/Cake/Model/Datasource/DboSource.php @@ -430,18 +430,27 @@ class DboSource extends DataSource { } } - $query = $this->_connection->prepare($sql, $prepareOptions); - $query->setFetchMode(PDO::FETCH_LAZY); - if (!$query->execute($params)) { - $this->_results = $query; - $query->closeCursor(); - return false; + try { + $query = $this->_connection->prepare($sql, $prepareOptions); + $query->setFetchMode(PDO::FETCH_LAZY); + if (!$query->execute($params)) { + $this->_results = $query; + $query->closeCursor(); + return false; + } + if (!$query->columnCount()) { + $query->closeCursor(); + return true; + } + return $query; + } catch (PDOException $e) { + if (isset($query->queryString)) { + $e->queryString = $query->queryString; + } else { + $e->queryString = $sql; + } + throw $e; } - if (!$query->columnCount()) { - $query->closeCursor(); - return true; - } - return $query; } /** diff --git a/lib/Cake/Test/Case/Error/ExceptionRendererTest.php b/lib/Cake/Test/Case/Error/ExceptionRendererTest.php index 005fdb3dc..ea356c81f 100644 --- a/lib/Cake/Test/Case/Error/ExceptionRendererTest.php +++ b/lib/Cake/Test/Case/Error/ExceptionRendererTest.php @@ -665,4 +665,27 @@ class ExceptionRendererTest extends CakeTestCase { $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('/
+ : + getMessage()); ?> +
+queryString)) : ?> ++ : + queryString; ?> +
+ +params)) : ?> + : + params); ?> + ++ : + +
+element('exception_stack_trace'); ?>