Browse Source

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
mark_story 12 years ago
parent
commit
2c85da44da
2 changed files with 17 additions and 1 deletions
  1. 5 1
      src/View/Cell.php
  2. 12 0
      tests/TestCase/View/CellTest.php

+ 5 - 1
src/View/Cell.php

@@ -160,7 +160,11 @@ abstract class Cell {
  * @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();
+		}
 	}
 
 /**

+ 12 - 0
tests/TestCase/View/CellTest.php

@@ -73,6 +73,18 @@ class CellTest extends TestCase {
 	}
 
 /**
+ * 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.
  *
  * @return void