Skip to content

Commit

Permalink
Add error handling for Cell::__toString()
Browse files Browse the repository at this point in the history
Because __toString methods cannot emit exceptions, we render a string
error. This gives the developer some feedback about what is broken.

Refs #3664
  • Loading branch information
markstory committed Jun 8, 2014
1 parent 4ef17b5 commit 2c85da4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/View/Cell.php
Expand Up @@ -160,7 +160,11 @@ public function render($template = null) {
* @return string Rendered cell
*/
public function __toString() {
return $this->render();
try {
return $this->render();
} catch (\Exception $e) {
return "Error: Could not render cell - " . $e->getMessage();
}
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/TestCase/View/CellTest.php
Expand Up @@ -72,6 +72,18 @@ public function testCellRender() {
$this->assertContains('<h2>Suspendisse gravida neque</h2>', $render);
}

/**
* Test __toString() hitting an error when rendering views.
*
* @return void
*/
public function testCellImplictRenderWithError() {
$cell = $this->View->cell('Articles::teaserList');
$cell->template = 'nope';
$output = "{$cell}";
$this->assertStringStartsWith("Error: Could not render cell - View file", $output);
}

/**
* Tests that we are able pass multiple arguments to cell methods.
*
Expand Down

0 comments on commit 2c85da4

Please sign in to comment.