Skip to content

Commit

Permalink
Remove Exception coercion.
Browse files Browse the repository at this point in the history
Just change error message and template instead.
  • Loading branch information
ADmad committed Apr 20, 2014
1 parent ed7b138 commit dc0da6e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
47 changes: 36 additions & 11 deletions src/Error/ExceptionRenderer.php
Expand Up @@ -79,15 +79,6 @@ class ExceptionRenderer {
* @param \Exception $exception Exception
*/
public function __construct(\Exception $exception) {
if (!Configure::read('debug') && !($exception instanceof Error\HttpException)) {
$code = $this->_code($exception);
if ($code < 500) {
$exception = new Error\NotFoundException();
} else {
$exception = new Error\InternalErrorException();
}
}

$this->controller = $this->_getController($exception);
$this->error = $exception;
}
Expand Down Expand Up @@ -136,7 +127,7 @@ public function render() {
$exception = $this->error;
$code = $this->_code($exception);
$template = $this->_template($exception, $code);
$message = $this->error->getMessage();
$message = $this->_message($exception, $code);
$url = $this->controller->request->here();

$this->controller->response->statusCode($code);
Expand All @@ -156,12 +147,46 @@ public function render() {
}

/**
* Get template for rendering exception info
* Get error message.
*
* @param Exception $exception Exception
* @param int $code Error code
* @return string Error message
*/
protected function _message(\Exception $exception, $code) {
$message = $this->error->getMessage();

if (!Configure::read('debug') &&
!($exception instanceof Error\HttpException)
) {
if ($code < 500) {
$message = __d('cake', 'Not Found');
} else {
$message = __d('cake', 'An Internal Error Has Occurred.');
}
}

return $message;
}

/**
* Get template for rendering exception info.
*
* @param \Exception $exception
* @param int $code Error code
* @return string Template name
*/
protected function _template(\Exception $exception, $code) {
if (!Configure::read('debug') &&
!($exception instanceof Error\HttpException)
) {
$template = 'error500';
if ($code < 500) {
$template = 'error400';
}
return $this->template = $template;
}

list(, $baseClass) = namespaceSplit(get_class($exception));
$baseClass = substr($baseClass, 0, -9);
$template = Inflector::variable($baseClass);
Expand Down
16 changes: 12 additions & 4 deletions tests/TestCase/Error/ExceptionRendererTest.php
Expand Up @@ -197,17 +197,25 @@ public function testConstruction() {
}

/**
* test that exception gets coerced when debug = 0
* test that exception message gets coerced when debug = 0
*
* @return void
*/
public function testExceptionCoercion() {
public function testExceptionMessageCoercion() {
Configure::write('debug', false);
$exception = new MissingActionException('Page not found');
$exception = new MissingActionException('Secret info not to be leaked');
$ExceptionRenderer = new ExceptionRenderer($exception);

$this->assertInstanceOf('Cake\Controller\ErrorController', $ExceptionRenderer->controller);
$this->assertTrue($ExceptionRenderer->error instanceof Error\NotFoundException);
$this->assertEquals($exception, $ExceptionRenderer->error);

ob_start();
$ExceptionRenderer->render();
$result = ob_get_clean();

$this->assertEquals('error400', $ExceptionRenderer->template);
$this->assertContains('Not Found', $result);
$this->assertNotContains('Secret info not to be leaked', $result);
}

/**
Expand Down

0 comments on commit dc0da6e

Please sign in to comment.