From d198230e61b491587a4d29c69c915c9801cca5ae Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sat, 4 Sep 2010 19:06:10 -0400 Subject: [PATCH] Fixing issue where errors that did not have a 500+ code would use the incorrect status code. --- cake/libs/error_handler.php | 7 ++++--- cake/tests/cases/libs/error_handler.test.php | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cake/libs/error_handler.php b/cake/libs/error_handler.php index 611f08982..2b2277521 100644 --- a/cake/libs/error_handler.php +++ b/cake/libs/error_handler.php @@ -85,8 +85,8 @@ class ErrorHandler { /** * Creates the controller to perform rendering on the error response. - * If the error is a CakeException it will be converted to either a 404 or a 500 - * type error depending on the code used to construct the error. + * If the error is a CakeException it will be converted to either a 400 or a 500 + * code error depending on the code used to construct the error. * * @param string $method Method producing the error * @param array $messages Error messages @@ -228,7 +228,8 @@ class ErrorHandler { */ public function error500($error) { $url = Router::normalize($this->controller->request->here); - $this->controller->response->statusCode($error->getCode()); + $code = ($error->getCode() > 500) ? $error->getCode() : 500; + $this->controller->response->statusCode($code); $this->controller->set(array( 'name' => __('An Internal Error Has Occurred'), 'message' => h($url), diff --git a/cake/tests/cases/libs/error_handler.test.php b/cake/tests/cases/libs/error_handler.test.php index 170d246c1..7915c14bc 100644 --- a/cake/tests/cases/libs/error_handler.test.php +++ b/cake/tests/cases/libs/error_handler.test.php @@ -341,6 +341,12 @@ class ErrorHandlerTest extends CakeTestCase { function testUnknownExceptionTypeWithExceptionThatHasA400Code() { $exception = new MissingWidgetThingException('coding fail.'); $ErrorHandler = new ErrorHandler($exception); + $ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode')); + $ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(404); + + ob_start(); + $ErrorHandler->render(); + $results = ob_get_clean(); $this->assertFalse(method_exists($ErrorHandler, 'missingWidgetThing'), 'no method should exist.'); $this->assertEquals('error400', $ErrorHandler->method, 'incorrect method coercion.'); @@ -354,6 +360,12 @@ class ErrorHandlerTest extends CakeTestCase { function testUnknownExceptionTypeWithNoCodeIsA500() { $exception = new OutOfBoundsException('foul ball.'); $ErrorHandler = new ErrorHandler($exception); + $ErrorHandler->controller->response = $this->getMock('CakeResponse', array('statusCode')); + $ErrorHandler->controller->response->expects($this->once())->method('statusCode')->with(500); + + ob_start(); + $ErrorHandler->render(); + $results = ob_get_clean(); $this->assertEquals('error500', $ErrorHandler->method, 'incorrect method coercion.'); }