Skip to content

Commit 4fc8d47

Browse files
committed
Add ability to pass exceptions as flash message
1 parent 2abaf3d commit 4fc8d47

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/Controller/Component/FlashComponent.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public function __construct(ComponentRegistry $collection, array $config = []) {
4343
}
4444

4545
public function set($message, $element = null, array $params = array(), $key = 'flash') {
46+
if ($message instanceof \Exception) {
47+
$message = $message->getMessage();
48+
}
4649
$this->_writeFlash($message, 'info', $params + compact('element', 'key'));
4750
}
4851

tests/TestCase/Controller/Component/FlashComponentTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,19 @@ public function testSet() {
115115
$result = $this->Session->read('Message.foobar');
116116
$this->assertEquals($expected, $result);
117117
}
118+
119+
/**
120+
* testSetWithException method
121+
*
122+
* @return void
123+
* @covers \Cake\Controller\Component\FlashComponent::set
124+
*/
125+
public function testSetWithException() {
126+
$this->assertNull($this->Session->read('Message.flash'));
127+
128+
$this->Flash->set(new \Exception('This is a test message'));
129+
$expected = ['message' => 'This is a test message', 'params' => ['element' => null], 'type' => 'info'];
130+
$result = $this->Session->read('Message.flash');
131+
$this->assertEquals($expected, $result);
132+
}
118133
}

0 commit comments

Comments
 (0)