Skip to content

Commit 24ee4f2

Browse files
author
vpaladiychuk
committed
MAGETWO-34988: Implement exception handling in dispatch() method
1 parent 2d2f63e commit 24ee4f2

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

app/etc/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,6 +1074,12 @@
10741074
</argument>
10751075
</arguments>
10761076
</type>
1077+
<type name="Magento\Framework\App\FrontController">
1078+
<arguments>
1079+
<argument name="appState" xsi:type="object">Magento\Framework\App\State\Proxy</argument>
1080+
<argument name="messageManager" xsi:type="object">Magento\Framework\Message\ManagerInterface\Proxy</argument>
1081+
</arguments>
1082+
</type>
10771083
<type name="Magento\Framework\Webapi\Rest\Request\DeserializerFactory">
10781084
<arguments>
10791085
<argument name="deserializers" xsi:type="array">

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function dispatch(RequestInterface $request)
6767
$routingCycleCounter = 0;
6868
$result = null;
6969
while (!$request->isDispatched() && $routingCycleCounter++ < 100) {
70-
$result = $this->matchAction($request);
70+
$result = $this->processRequest($request);
7171
}
7272
\Magento\Framework\Profiler::stop('routers_match');
7373
if ($routingCycleCounter > 100) {
@@ -81,23 +81,27 @@ public function dispatch(RequestInterface $request)
8181
*
8282
* @param \Exception $e
8383
* @param \Magento\Framework\App\ActionInterface $actionInstance
84-
* @param string $message
8584
* @return \Magento\Framework\Controller\Result\Redirect
8685
*/
87-
protected function handleException($e, $actionInstance, $message)
86+
protected function handleException($e, $actionInstance)
8887
{
89-
$this->messageManager->addError($message);
88+
$needToMaskDisplayMessage = !($e instanceof \Magento\Framework\Exception\LocalizedException)
89+
&& ($this->appState->getMode() != State::MODE_DEVELOPER);
90+
$displayMessage = $needToMaskDisplayMessage
91+
? (string)new \Magento\Framework\Phrase('An error occurred while processing your request')
92+
: $e->getMessage();
93+
$this->messageManager->addError($displayMessage);
9094
$this->logger->critical($e->getMessage());
9195
return $actionInstance->getDefaultRedirect();
9296
}
9397

9498
/**
95-
* Match action, dispatch
99+
* Route request and dispatch it
96100
*
97101
* @param RequestInterface $request
98-
* @return \Magento\Framework\Controller\Result\Redirect
102+
* @return ResponseInterface|\Magento\Framework\Controller\ResultInterface|null
99103
*/
100-
protected function matchAction(RequestInterface $request)
104+
protected function processRequest(RequestInterface $request)
101105
{
102106
$result = null;
103107
/** @var \Magento\Framework\App\RouterInterface $router */
@@ -107,24 +111,20 @@ protected function matchAction(RequestInterface $request)
107111
if ($actionInstance) {
108112
$request->setDispatched(true);
109113
$actionInstance->getResponse()->setNoCacheHeaders();
110-
$result = $actionInstance->dispatch($request);
114+
try {
115+
$result = $actionInstance->dispatch($request);
116+
} catch (Action\NotFoundException $e) {
117+
throw $e;
118+
} catch (\Exception $e) {
119+
$result = $this->handleException($e, $actionInstance);
120+
}
111121
break;
112122
}
113123
} catch (Action\NotFoundException $e) {
114124
$request->initForward();
115125
$request->setActionName('noroute');
116126
$request->setDispatched(false);
117127
break;
118-
} catch (\Magento\Framework\Exception\LocalizedException $e) {
119-
$result = $this->handleException($e, $actionInstance, $e->getMessage());
120-
break;
121-
} catch (\Exception $e) {
122-
// @todo Message should be clarified
123-
$message = $this->appState->getMode() == State::MODE_DEVELOPER
124-
? $e->getMessage()
125-
: (string)new \Magento\Framework\Phrase('An error occurred while processing your request');
126-
$result = $this->handleException($e, $actionInstance, $message);
127-
break;
128128
}
129129
}
130130
return $result;

0 commit comments

Comments
 (0)