Skip to content

Commit

Permalink
Make exception rendererer more resiliant
Browse files Browse the repository at this point in the history
Exceptions in beforeRender() should render correct error pages.

Fixes #3235
  • Loading branch information
markstory committed Sep 28, 2012
1 parent 99edef0 commit 393849a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/Cake/Error/ExceptionRenderer.php
Expand Up @@ -290,11 +290,11 @@ protected function _outputMessageSafe($template) {
$this->controller->layoutPath = null;
$this->controller->subDir = null;
$this->controller->viewPath = 'Errors/';
$this->controller->viewClass = 'View';
$this->controller->layout = 'error';
$this->controller->helpers = array('Form', 'Html', 'Session');

$this->controller->render($template);
$view = new View($this->controller);
$this->controller->response->body($view->render($template, 'error'));
$this->controller->response->type('html');
$this->controller->response->send();
}
Expand Down
57 changes: 40 additions & 17 deletions lib/Cake/Test/Case/Error/ExceptionRendererTest.php
Expand Up @@ -670,25 +670,49 @@ public function testMissingRenderSafe() {
$exception = new MissingHelperException(array('class' => 'Fail'));
$ExceptionRenderer = new ExceptionRenderer($exception);

$ExceptionRenderer->controller = $this->getMock('Controller');
$ExceptionRenderer->controller = $this->getMock('Controller', array('render'));
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
$ExceptionRenderer->controller->expects($this->at(2))
$ExceptionRenderer->controller->expects($this->at(0))
->method('render')
->with('missingHelper')
->will($this->throwException($exception));

$ExceptionRenderer->controller->expects($this->at(4))
->method('render')
->with('error500')
->will($this->returnValue(true));
$response = $this->getMock('CakeResponse');
$response->expects($this->once())
->method('body')
->with($this->stringContains('Helper class Fail'));

$ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
$ExceptionRenderer->controller->response = $response;
$ExceptionRenderer->render();
sort($ExceptionRenderer->controller->helpers);
$this->assertEquals(array('Form', 'Html', 'Session'), $ExceptionRenderer->controller->helpers);
}

/**
* Test that exceptions in beforeRender() are handled by outputMessageSafe
*
* @return void
*/
public function testRenderExceptionInBeforeRender() {
$exception = new NotFoundException('Not there, sorry');
$ExceptionRenderer = new ExceptionRenderer($exception);

$ExceptionRenderer->controller = $this->getMock('Controller', array('beforeRender'));
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');
$ExceptionRenderer->controller->expects($this->any())
->method('beforeRender')
->will($this->throwException($exception));

$response = $this->getMock('CakeResponse');
$response->expects($this->once())
->method('body')
->with($this->stringContains('Not there, sorry'));

$ExceptionRenderer->controller->response = $response;
$ExceptionRenderer->render();
}

/**
* Test that missing subDir/layoutPath don't cause other fatal errors.
*
Expand All @@ -698,32 +722,31 @@ public function testMissingSubdirRenderSafe() {
$exception = new NotFoundException();
$ExceptionRenderer = new ExceptionRenderer($exception);

$ExceptionRenderer->controller = $this->getMock('Controller');
$ExceptionRenderer->controller = $this->getMock('Controller', array('render'));
$ExceptionRenderer->controller->helpers = array('Fail', 'Boom');
$ExceptionRenderer->controller->layoutPath = 'json';
$ExceptionRenderer->controller->subDir = 'json';
$ExceptionRenderer->controller->viewClass = 'Json';
$ExceptionRenderer->controller->request = $this->getMock('CakeRequest');

$ExceptionRenderer->controller->expects($this->at(1))
$ExceptionRenderer->controller->expects($this->once())
->method('render')
->with('error400')
->will($this->throwException($exception));

$ExceptionRenderer->controller->expects($this->at(3))
->method('render')
->with('error500')
->will($this->returnValue(true));

$ExceptionRenderer->controller->response = $this->getMock('CakeResponse');
$ExceptionRenderer->controller->response->expects($this->once())
$response = $this->getMock('CakeResponse');
$response->expects($this->once())
->method('body')
->with($this->stringContains('Not Found'));
$response->expects($this->once())
->method('type')
->with('html');

$ExceptionRenderer->controller->response = $response;

$ExceptionRenderer->render();
$this->assertEquals('', $ExceptionRenderer->controller->layoutPath);
$this->assertEquals('', $ExceptionRenderer->controller->subDir);
$this->assertEquals('View', $ExceptionRenderer->controller->viewClass);
$this->assertEquals('Errors/', $ExceptionRenderer->controller->viewPath);
}

Expand Down

0 comments on commit 393849a

Please sign in to comment.