From 2c85da44da2c9bbf6fc4a52ba990660f554d320d Mon Sep 17 00:00:00 2001 From: mark_story Date: Sun, 8 Jun 2014 15:06:02 -0400 Subject: [PATCH] Add error handling for Cell::__toString() Because __toString methods cannot emit exceptions, we render a string error. This gives the developer some feedback about what is broken. Refs #3664 --- src/View/Cell.php | 6 +++++- tests/TestCase/View/CellTest.php | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/View/Cell.php b/src/View/Cell.php index 3b5939ff0b8..1fe687482da 100644 --- a/src/View/Cell.php +++ b/src/View/Cell.php @@ -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(); + } } /** diff --git a/tests/TestCase/View/CellTest.php b/tests/TestCase/View/CellTest.php index 648cba2a7ef..a1ed69abdae 100644 --- a/tests/TestCase/View/CellTest.php +++ b/tests/TestCase/View/CellTest.php @@ -72,6 +72,18 @@ public function testCellRender() { $this->assertContains('

Suspendisse gravida neque

', $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. *