Browse Source

Merge pull request #3968 from ADmad/3.0-flash

Set exception code in flash params.
Mark Story 11 years ago
parent
commit
1ff7bf510a

+ 4 - 1
src/Controller/Component/FlashComponent.php

@@ -66,7 +66,9 @@ class FlashComponent extends Component {
  * - `element` The element used to render the flash message
  * - `params` An array of variables to make available when using an element
  *
- * @param string $message Message to be flashed
+ * @param string|\Exception $message Message to be flashed. If an instance
+ *   of \Exception the exception message will be used and code will be set
+ *   in params.
  * @param array $options An array of options
  * @return void
  */
@@ -74,6 +76,7 @@ class FlashComponent extends Component {
 		$opts = array_merge($this->_defaultConfig, $options);
 
 		if ($message instanceof \Exception) {
+			$opts['params'] += ['code' => $message->getCode()];
 			$message = $message->getMessage();
 		}
 

+ 2 - 2
tests/TestCase/Controller/Component/FlashComponentTest.php

@@ -111,12 +111,12 @@ class FlashComponentTest extends TestCase {
 	public function testSetWithException() {
 		$this->assertNull($this->Session->read('Flash.flash'));
 
-		$this->Flash->set(new \Exception('This is a test message'));
+		$this->Flash->set(new \Exception('This is a test message', 404));
 		$expected = [
 			'message' => 'This is a test message',
 			'key' => 'flash',
 			'element' => null,
-			'params' => []
+			'params' => ['code' => 404]
 		];
 		$result = $this->Session->read('Flash.flash');
 		$this->assertEquals($expected, $result);