Skip to content

Commit 9106a37

Browse files
author
vpaladiychuk
committed
MAGETWO-34988: Implement exception handling in dispatch() method
1 parent 44e90c4 commit 9106a37

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

lib/internal/Magento/Framework/App/FrontController.php

100644100755
Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,41 @@ class FrontController implements FrontControllerInterface
1414
*/
1515
protected $_routerList;
1616

17+
/**
18+
* Application state
19+
*
20+
* @var State
21+
*/
22+
protected $appState;
23+
24+
/**
25+
* Message manager
26+
*
27+
* @var \Magento\Framework\Message\ManagerInterface
28+
*/
29+
protected $messageManager;
30+
31+
/**
32+
* @var \Psr\Log\LoggerInterface
33+
*/
34+
protected $logger;
35+
1736
/**
1837
* @param RouterList $routerList
38+
* @param State $appState
39+
* @param \Magento\Framework\Message\ManagerInterface $messageManager
40+
* @param \Psr\Log\LoggerInterface $logger
1941
*/
20-
public function __construct(RouterList $routerList)
21-
{
42+
public function __construct(
43+
RouterList $routerList,
44+
State $appState,
45+
\Magento\Framework\Message\ManagerInterface $messageManager,
46+
\Psr\Log\LoggerInterface $logger
47+
) {
2248
$this->_routerList = $routerList;
49+
$this->appState = $appState;
50+
$this->messageManager = $messageManager;
51+
$this->logger = $logger;
2352
}
2453

2554
/**
@@ -50,6 +79,16 @@ public function dispatch(RequestInterface $request)
5079
$request->setActionName('noroute');
5180
$request->setDispatched(false);
5281
break;
82+
} catch (\Magento\Framework\LocalizedException $e) {
83+
$result = $this->handleException($e, $actionInstance, $e->getMessage());
84+
break;
85+
} catch (\Exception $e) {
86+
// @todo Message should be clarified
87+
$message = $this->appState->getMode() == State::MODE_DEVELOPER
88+
? $e->getMessage()
89+
: (string)__('An error occurred while processing your request');
90+
$result = $this->handleException($e, $actionInstance, $message);
91+
break;
5392
}
5493
}
5594
}
@@ -59,4 +98,19 @@ public function dispatch(RequestInterface $request)
5998
}
6099
return $result;
61100
}
101+
102+
/**
103+
* Handle exception
104+
*
105+
* @param \Exception $e
106+
* @param \Magento\Framework\App\ActionInterface $actionInstance
107+
* @param string $message
108+
* @return \Magento\Framework\Controller\Result\Redirect
109+
*/
110+
protected function handleException($e, $actionInstance, $message)
111+
{
112+
$this->messageManager->addError($message);
113+
$this->logger->critical($e->getMessage());
114+
return $actionInstance->getDefaultRedirect();
115+
}
62116
}

lib/internal/Magento/Framework/App/Test/Unit/FrontControllerTest.php

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ protected function setUp()
3838

3939
$this->router = $this->getMock('Magento\Framework\App\RouterInterface');
4040
$this->routerList = $this->getMock('Magento\Framework\App\RouterList', [], [], '', false);
41-
$this->model = new \Magento\Framework\App\FrontController($this->routerList);
41+
$this->model = (new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this))
42+
->getObject('Magento\Framework\App\FrontController', ['routerList' => $this->routerList]);
4243
}
4344

4445
/**

0 commit comments

Comments
 (0)