forked from EasyCorp/EasyAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionListener.php
More file actions
198 lines (168 loc) · 6.37 KB
/
ExceptionListener.php
File metadata and controls
198 lines (168 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/*
* This file is part of the EasyAdminBundle.
*
* (c) Javier Eguiluz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JavierEguiluz\Bundle\EasyAdminBundle\EventListener;
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\BaseException;
use JavierEguiluz\Bundle\EasyAdminBundle\Exception\FlattenException;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener as BaseExceptionListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
/**
* This listener allows to display customized error pages in the production
* environment.
*
* @author Javier Eguiluz <[email protected]>
* @author Maxime Steinhausser <[email protected]>
*/
class ExceptionListener extends BaseExceptionListener
{
/** @var EngineInterface */
private $templating;
/** @var array */
private $easyAdminConfig;
private $currentEntityName;
public function __construct(EngineInterface $templating, array $easyAdminConfig, $controller, LoggerInterface $logger = null)
{
$this->templating = $templating;
$this->easyAdminConfig = $easyAdminConfig;
parent::__construct($controller, $logger);
}
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$this->currentEntityName = $event->getRequest()->query->get('entity', null);
if (!$exception instanceof BaseException) {
return;
}
if (!$this->isLegacySymfony()) {
parent::onKernelException($event);
} else {
$response = $this->legacyOnKernelException($event);
$event->setResponse($response);
}
}
/**
* @param FlattenException $exception
*
* @return Response
*/
public function showExceptionPageAction(FlattenException $exception)
{
$entityConfig = isset($this->easyAdminConfig['entities'][$this->currentEntityName])
? $this->easyAdminConfig['entities'][$this->currentEntityName] : null;
$exceptionTemplatePath = isset($entityConfig['templates']['exception'])
? $entityConfig['templates']['exception']
: isset($this->easyAdminConfig['design']['templates']['exception'])
? $this->easyAdminConfig['design']['templates']['exception']
: '@EasyAdmin/default/exception.html.twig';
return $this->templating->renderResponse(
$exceptionTemplatePath,
array('exception' => $exception),
Response::create()->setStatusCode($exception->getStatusCode())
);
}
/**
* {@inheritdoc}
*/
protected function logException(\Exception $exception, $message, $original = true)
{
if (!$exception instanceof BaseException) {
parent::logException($exception, $message, $original);
return;
}
if (null !== $this->logger) {
if ($exception->getStatusCode() >= 500) {
$this->logger->critical($message, array('exception' => $exception));
} else {
$this->logger->error($message, array('exception' => $exception));
}
}
}
/**
* {@inheritdoc}
*/
protected function duplicateRequest(\Exception $exception, Request $request)
{
if (!$this->isLegacySymfony()) {
$request = parent::duplicateRequest($exception, $request);
} else {
$request = $this->legacyDuplicateRequest($request);
}
$request->attributes->set('exception', FlattenException::create($exception));
return $request;
}
/**
* Utility method needed for BC reasons with Symfony 2.3
* Code copied from Symfony\Component\HttpKernel\EventListener\ExceptionListener
*
* @param GetResponseForExceptionEvent $event
*
* @return Response
*
* @throws \Exception
*/
private function legacyOnKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
$this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
$request = $this->duplicateRequest($exception, $event->getRequest());
try {
return $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
} catch (\Exception $e) {
$this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()));
$wrapper = $e;
while ($prev = $wrapper->getPrevious()) {
if ($exception === $wrapper = $prev) {
throw $e;
}
}
$prev = new \ReflectionProperty('Exception', 'previous');
$prev->setAccessible(true);
$prev->setValue($wrapper, $exception);
throw $e;
}
}
/**
* Utility method needed for BC reasons with Symfony 2.3
* Code copied from Symfony\Component\HttpKernel\EventListener\ExceptionListener.
*
* @param Request $request
*
* @return Request
*/
private function legacyDuplicateRequest(Request $request)
{
$attributes = array(
'_controller' => $this->controller,
'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
'format' => $request->getRequestFormat(),
);
$request = $request->duplicate(null, null, $attributes);
$request->setMethod('GET');
return $request;
}
/**
* Returns true if Symfony version is considered legacy (e.g. 2.3)
*
* @return bool
*/
private function isLegacySymfony()
{
return 2 === Kernel::MAJOR_VERSION && 3 === Kernel::MINOR_VERSION;
}
}