8000 HttpKernel: catch Throwable · symfony/symfony@3aecd3c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3aecd3c

Browse files
committed
HttpKernel: catch Throwable
1 parent 5f64d32 commit 3aecd3c

File tree

9 files changed

+124
-34
lines changed

9 files changed

+124
-34
lines changed

src/Symfony/Component/HttpKernel/DependencyInjection/ContainerAwareHttpKernel.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
7171
$this->container->set('request', null, 'request');
7272
$this->container->leaveScope('request');
7373

74+
throw $e;
75+
} catch (\Throwable $e) {
76+
$this->container->set('request', null, 'request');
77+
$this->container->leaveScope('request');
78+
7479
throw $e;
7580
}
7681

src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\HttpKernel\EventListener;
1313

1414
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\HttpKernel\Exception\FatalThrowableError;
1516
use Symfony\Component\Debug\Exception\FlattenException;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
@@ -49,6 +50,11 @@ public function onKernelException(GetResponseForExceptionEvent $event)
4950
try {
5051
$response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
5152
} catch (\Exception $e) {
53+
} catch (\Throwable $e) {
54+
$e = new FatalThrowableError($e);
55+
}
56+
57+
if (isset($e)) {
5258
$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()));
5359

5460
$wrapper = $e;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Exception;
13+
14+
class FatalThrowableError extends \ErrorException
15+
{
16+
public function __construct(\Throwable $e)
17+
{
18+
if ($e instanceof \ParseError) {
19+
$message = 'Parse error: '.$e->getMessage();
20+
$severity = E_PARSE;
21+
} elseif ($e instanceof \TypeError) {
22+
$message = 'Type error: '.$e->getMessage();
23+
$severity = E_RECOVERABLE_ERROR;
24+
} else {
25+
$message = 'Fatal error: '.$e->getMessage();
26+
$severity = E_ERROR;
27+
}
28+
29+
\ErrorException::__construct(
30+
$message,
31+
$e->getCode(),
32+
$severity,
33+
$e->getFile(),
34+
$e->getLine()
35+
);
36+
37+
$this->setTrace($e->getTrace());
38+
}
39+
40+
protected function setTrace($trace)
41+
{
42+
$traceReflector = new \ReflectionProperty('Exception', 'trace');
43+
$traceReflector->setAccessible(true);
44+
$traceReflector->setValue($this, $trace);
45+
}
46+
}

src/Symfony/Component/HttpKernel/Fragment/InlineFragmentRenderer.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Fragment;
1313

14+
use Symfony\Component\HttpKernel\Exception\FatalThrowableError;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -84,30 +85,33 @@ public function render($uri, Request $request, array $options = array())
8485
try {
8586
return $this->kernel->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false);
8687
} catch (\Exception $e) {
87-
// we dispatch the exception event to trigger the logging
88-
// the response that comes back is simply ignored
89-
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
90-
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
88+
} catch (\Throwable $e) {
89+
$e = new FatalThrowableError($e);
90+
}
9191

92-
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
93-
}
92+
// we dispatch the exception event to trigger the logging
93+
// the response that comes back is simply ignored
94+
if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
95+
$event = new GetResponseForExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
9496

95-
// let's clean up the output buffers that were created by the sub-request
96-
Response::closeOutputBuffers($level, false);
97+
$this->dispatcher->dispatch(KernelEvents::EXCEPTION, $event);
98+
}
9799

98-
if (isset($options['alt'])) {
99-
$alt = $options['alt'];
100-
unset($options['alt']);
100+
// let's clean up the output buffers that were created by the sub-request
101+
Response::closeOutputBuffers($level, false);
101102

102-
return $this->render($alt, $request, $options);
103-
}
103+
if (isset($options['alt'])) {
104+
$alt = $options['alt'];
105+
unset($options['alt']);
104106

105-
if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
106-
throw $e;
107-
}
107+
return $this->render($alt, $request, $options);
108+
}
108109

109-
return new Response();
110+
if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
111+
throw $e;
110112
}
113+
114+
return new Response();
111115
}
112116

113117
protected function createSubRequest($uri, Request $request)

src/Symfony/Component/HttpKernel/HttpCache/Esi.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,15 @@ public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
283283

284284
return $response->getContent();
285285
} catch (\Exception $e) {
286-
if ($alt) {
287-
return $this->handle($cache, $alt, '', $ignoreErrors);
288-
}
286+
} catch (\Throwable $e) {
287+
}
289288

290-
if (!$ignoreErrors) {
291-
throw $e;
292-
}
289+
if ($alt) {
290+
return $this->handle($cache, $alt, '', $ignoreErrors);
291+
}
292+
293+
if (!$ignoreErrors) {
294+
throw $e;
293295
}
294296
}
295297
}

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ protected function invalidate(Request $request, $catch = false)
290290

291291
$this->record($request, 'invalidate');
292292
} catch (\Exception $e) {
293+
} catch (\Throwable $e) {
294+
}
295+
296+
if (isset($e)) {
293297
$this->record($request, 'invalidate-failed');
294298

295299
if ($this->options['debug']) {
@@ -329,6 +333,10 @@ protected function lookup(Request $request, $catch = false)
329333
try {
330334
$entry = $this->store->lookup($request);
331335
} catch (\Exception $e) {
336+
} catch (\Throwable $e) {
337+
}
338+
339+
if (isset($e)) {
332340
$this->record($request, 'lookup-failed');
333341

334342
if ($this->options['debug']) {
@@ -607,6 +615,10 @@ protected function store(Request $request, Response $response)
607615

608616
$response->headers->set('Age', $response->getAge());
609617
} catch (\Exception $e) {
618+
} catch (\Throwable $e) {
619+
}
620+
621+
if (isset($e)) {
610622
$this->record($request, 'store-failed');
611623

612624
if ($this->options['debug']) {

src/Symfony/Component/HttpKernel/HttpCache/Ssi.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,15 @@ public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
182182

183183
return $response->getContent();
184184
} catch (\Exception $e) {
185-
if ($alt) {
186-
return $this->handle($cache, $alt, '', $ignoreErrors);
187-
}
185+
} catch (\Throwable $e) {
186+
}
188187

189-
if (!$ignoreErrors) {
190-
throw $e;
191-
}
188+
if ($alt) {
189+
return $this->handle($cache, $alt, '', $ignoreErrors);
190+
}
191+
192+
if (!$ignoreErrors) {
193+
throw $e;
192194
}
193195
}
194196
}

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel;
1313

14+
use Symfony\Component\HttpKernel\Exception\FatalThrowableError;
1415
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
1516
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1617
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
@@ -61,14 +62,20 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
6162
try {
6263
return $this->handleRaw($request, $type);
6364
} catch (\Exception $e) {
64-
if (false === $catch) {
65-
$this->finishRequest($request, $type);
65+
} catch (\Throwable $e) {
66+
}
6667

67-
throw $e;
68-
}
68+
if (false === $catch) {
69+
$this->finishRequest($request, $type);
70+
71+
throw $e;
72+
}
6973

70-
return $this->handleException($e, $request, $type);
74+
if ($e instanceof \Throwable) {
75+
$e = new FatalThrowableError($e);
7176
}
77+
78+
return $this->handleException($e, $request, $type);
7279
}
7380

7481
/**
@@ -246,6 +253,8 @@ private function handleException(\Exception $e, $request, $type)
246253
return $this->filterResponse($response, $request, $type);
247254
} catch (\Exception $e) {
248255
return $response;
256+
} catch (\Throwable $e) {
257+
return $response;
249258
}
250259
}
251260

src/Symfony/Component/HttpKernel/Profiler/PdoProfilerStorage.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ public function write(Profile $profile)
112112
$status = true;
113113
} catch (\Exception $e) {
114114
$status = false;
115+
} catch (\Throwable $e) {
116+
$status = false;
115117
}
116118

117119
$this->close($db);
@@ -176,6 +178,8 @@ protected function prepareStatement($db, $query)
176178
$stmt = $db->prepare($query);
177179
} catch (\Exception $e) {
178180
$stmt = false;
181+
} catch (\Throwable $e) {
182+
$stmt = false;
179183
}
180184

181185
if (false === $stmt) {

0 commit comments

Comments
 (0)
0