From f621bb7fe5c6e648630ac615b4e90a08d290708f Mon Sep 17 00:00:00 2001 From: David Steinsland Date: Thu, 15 Jan 2015 19:20:28 +0100 Subject: [PATCH] Fixed nesting when ExceptionRenderer throws exception --- lib/Cake/Error/ErrorHandler.php | 13 +++++-- lib/Cake/Test/Case/Error/ErrorHandlerTest.php | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Error/ErrorHandler.php b/lib/Cake/Error/ErrorHandler.php index e87c636d1..18efed6ce 100644 --- a/lib/Cake/Error/ErrorHandler.php +++ b/lib/Cake/Error/ErrorHandler.php @@ -125,6 +125,8 @@ class ErrorHandler { $e->getMessage(), $e->getTraceAsString() ); + + Configure::write('Exception.bail', true); trigger_error($message, E_USER_ERROR); } } @@ -249,10 +251,17 @@ class ErrorHandler { } if (Configure::read('debug')) { - call_user_func($exceptionHandler, new FatalErrorException($description, 500, $file, $line)); + $exception = new FatalErrorException($description, 500, $file, $line); } else { - call_user_func($exceptionHandler, new InternalErrorException()); + $exception = new InternalErrorException(); } + + if (Configure::read('Exception.bail')) { + throw $exception; + } + + call_user_func($exceptionHandler, $exception); + return false; } diff --git a/lib/Cake/Test/Case/Error/ErrorHandlerTest.php b/lib/Cake/Test/Case/Error/ErrorHandlerTest.php index 6ec656036..b2f87882d 100644 --- a/lib/Cake/Test/Case/Error/ErrorHandlerTest.php +++ b/lib/Cake/Test/Case/Error/ErrorHandlerTest.php @@ -20,6 +20,16 @@ App::uses('ErrorHandler', 'Error'); App::uses('Controller', 'Controller'); App::uses('Router', 'Routing'); +/** + * A faulty ExceptionRenderer to test nesting. + */ +class FaultyExceptionRenderer extends ExceptionRenderer { + + public function render() { + throw new Exception('Error from renderer.'); + } +} + /** * ErrorHandlerTest class * @@ -320,4 +330,28 @@ class ErrorHandlerTest extends CakeTestCase { $this->assertContains('[FatalErrorException] Something wrong', $log[1], 'message missing.'); } +/** + * testExceptionRendererNestingDebug method + * + * @expectedException FatalErrorException + * @return void + */ + public function testExceptionRendererNestingDebug() { + Configure::write('debug', 2); + Configure::write('Exception.renderer', 'FaultyExceptionRenderer'); + ErrorHandler::handleFatalError(E_USER_ERROR, 'Initial error', __FILE__ ,__LINE__); + } + +/** + * testExceptionRendererNestingProduction method + * + * @expectedException InternalErrorException + * @return void + */ + public function testExceptionRendererNestingProduction() { + Configure::write('debug', 0); + Configure::write('Exception.renderer', 'FaultyExceptionRenderer'); + ErrorHandler::handleFatalError(E_USER_ERROR, 'Initial error', __FILE__ ,__LINE__); + } + }