Skip to content

Commit 584f4d6

Browse files
committed
Allow \Throwable to be rendered
1 parent 11fab74 commit 584f4d6

File tree

8 files changed

+79
-40
lines changed

8 files changed

+79
-40
lines changed

src/Symfony/Component/Console/Application.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -768,20 +768,39 @@ public static function getAbbreviations($names)
768768

769769
/**
770770
* Renders a caught exception.
771+
*
772+
* @deprecated since Symfony 4.4, use "renderThrowable()" instead
771773
*/
772774
public function renderException(\Exception $e, OutputInterface $output)
775+
{
776+
@trigger_error(sprintf('The "%s::renderException()" method is deprecated since Symfony 4.4, use "renderThrowable()" instead.', \get_class($this)));
777+
778+
$this->renderThrowable($e, $output);
779+
}
780+
781+
public function renderThrowable(\Throwable $e, OutputInterface $output): void
773782
{
774783
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
775784

776-
$this->doRenderException($e, $output);
785+
$this->doRenderThrowable($e, $output);
777786

778787
if (null !== $this->runningCommand) {
779788
$output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);
780789
$output->writeln('', OutputInterface::VERBOSITY_QUIET);
781790
}
782791
}
783792

793+
/**
794+
* @deprecated since Symfony 4.4, use "doRenderThrowable()" instead
795+
*/
784796
protected function doRenderException(\Exception $e, OutputInterface $output)
797+
{
798+
@trigger_error(sprintf('The "%s::doRenderException()" method is deprecated since Symfony 4.4, use "doRenderThrowable()" instead.', \get_class($this)));
799+
800+
$this->doRenderThrowable($e, $output);
801+
}
802+
803+
protected function doRenderThrowable(\Throwable $e, OutputInterface $output): void
785804
{
786805
do {
787806
$message = trim($e->getMessage());

src/Symfony/Component/HttpKernel/Event/GetResponseForExceptionEvent.php

+25-12
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,58 @@
1919
*/
2020
class GetResponseForExceptionEvent extends RequestEvent
2121
{
22-
/**
23-
* The exception object.
24-
*
25-
* @var \Exception
26-
*/
27-
private $exception;
22+
private $throwable;
2823

2924
/**
3025
* @var bool
3126
*/
3227
private $allowCustomResponseCode = false;
3328

34-
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, \Exception $e)
29+
public function __construct(HttpKernelInterface $kernel, Request $request, int $requestType, $e)
3530
{
3631
parent::__construct($kernel, $request, $requestType);
3732

38-
$this->setException($e);
33+
$this->throwable = $e;
3934
}
4035

4136
/**
42-
* Returns the thrown exception.
37+
* Returns the thrown exception / error.
38+
*
39+
* @deprecated since Symfony 4.4, use "getThrowable()" instead
4340
*
44-
* @return \Exception The thrown exception
41+
* @return \Throwable The thrown exception / error
4542
*/
4643
public function getException()
4744
{
48-
return $this->exception;
45+
@trigger_error(sprintf('The "%s::getException()" method is deprecated since Symfony 4.4, use "getThrowable()" instead.', \get_class($this)));
46+
47+
return $this->getThrowable();
48+
}
49+
50+
public function getThrowable(): \Throwable
51+
{
52+
return $this->throwable;
4953
}
5054

5155
/**
5256
* Replaces the thrown exception.
5357
*
5458
* This exception will be thrown if no response is set in the event.
5559
*
60+
* @deprecated since Symfony 4.4, use "setThrowable()" instead
61+
*
5662
* @param \Exception $exception The thrown exception
5763
*/
5864
public function setException(\Exception $exception)
5965
{
60-
$this->exception = $exception;
66+
@trigger_error(sprintf('The "%s::setException()" method is deprecated since Symfony 4.4, use "setThrowable()" instead.', \get_class($this)));
67+
68+
$this->setThrowable($exception);
69+
}
70+
71+
public function setThrowable(\Throwable $throwable): void
72+
{
73+
$this->throwable = $throwable;
6174
}
6275

6376
/**

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public function __construct($controller, LoggerInterface $logger = null, $debug
4242

4343
public function logKernelException(GetResponseForExceptionEvent $event)
4444
{
45-
$e = FlattenException::createFromThrowable($event->getException());
45+
$e = FlattenException::createFromThrowable($event->getThrowable());
4646

47-
$this->logException($event->getException(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
47+
$this->logThrowable($event->getThrowable(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
4848
}
4949

5050
public function onKernelException(GetResponseForExceptionEvent $event)
@@ -62,7 +62,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
6262
} catch (\Exception $e) {
6363
$f = FlattenException::createFromThrowable($e);
6464

65-
$this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', $f->getClass(), $f->getMessage(), $e->getFile(), $e->getLine()));
65+
$this->logThrowable($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', $f->getClass(), $f->getMessage(), $e->getFile(), $e->getLine()));
6666

6767
$prev = $e;
6868
do {
@@ -104,8 +104,15 @@ public static function getSubscribedEvents()
104104
*
105105
* @param \Exception $exception The \Exception instance
106106
* @param string $message The error message to log
107+
*
108+
* @deprecated since Symfony 4.4, use "logThrowable()" instead.
107109
*/
108110
protected function logException(\Exception $exception, $message)
111+
{
112+
@trigger_error(sprintf('The "%s::logException()" method is deprecated since Symfony 4.4, use "logThrowable()" instead.', \get_class($this)));
113+
}
114+
115+
protected function logThrowable(\Throwable $exception, string $message): void
109116
{
110117
if (null !== $this->logger) {
111118
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function onKernelRequest(GetResponseEvent $event)
144144

145145
public function onKernelException(GetResponseForExceptionEvent $event)
146146
{
147-
if (!$this->debug || !($e = $event->getException()) instanceof NotFoundHttpException) {
147+
if (!$this->debug || !($e = $event->getThrowable()) instanceof NotFoundHttpException) {
148148
return;
149149
}
150150

src/Symfony/Component/HttpKernel/HttpKernel.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
7676
throw $e;
7777
}
7878

79-
return $this->handleException($e, $request, $type);
79+
return $this->handleThrowable($e, $request, $type);
8080
}
8181
}
8282

@@ -91,13 +91,13 @@ public function terminate(Request $request, Response $response)
9191
/**
9292
* @internal
9393
*/
94-
public function terminateWithException(\Exception $exception, Request $request = null)
94+
public function terminateWithException(\Throwable $exception, Request $request = null)
9595
{
9696
if (!$request = $request ?: $this->requestStack->getMasterRequest()) {
9797
throw $exception;
9898
}
9999

100-
$response = $this->handleException($exception, $request, self::MASTER_REQUEST);
100+
$response = $this->handleThrowable($exception, $request, self::MASTER_REQUEST);
101101

102102
$response->sendHeaders();
103103
$response->sendContent();
@@ -203,19 +203,19 @@ private function finishRequest(Request $request, int $type)
203203
}
204204

205205
/**
206-
* Handles an exception by trying to convert it to a Response.
206+
* Handles a throwable by trying to convert it to a Response.
207207
*
208208
* @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
209209
*
210-
* @throws \Exception
210+
* @throws \Throwable
211211
*/
212-
private function handleException(\Exception $e, Request $request, int $type): Response
212+
private function handleThrowable(\Throwable $e, Request $request, int $type): Response
213213
{
214214
$event = new ExceptionEvent($this, $request, $type, $e);
215215
$this->dispatcher->dispatch($event, KernelEvents::EXCEPTION);
216216

217217
// a listener might have replaced the exception
218-
$e = $event->getException();
218+
$e = $event->getThrowable();
219219

220220
if (!$event->hasResponse()) {
221221
$this->finishRequest($request, $type);

src/Symfony/Component/HttpKernel/KernelEvents.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ final class KernelEvents
3030
const REQUEST = 'kernel.request';
3131

3232
/**
33-
* The EXCEPTION event occurs when an uncaught exception appears.
33+
* The EXCEPTION event occurs when an uncaught exception / error appears.
3434
*
35-
* This event allows you to create a response for a thrown exception or
36-
* to modify the thrown exception.
35+
* This event allows you to create a response for a thrown exception / error or
36+
* to modify the thrown exception / error.
3737
*
3838
* @Event("Symfony\Component\HttpKernel\Event\ExceptionEvent")
3939
*/

src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function unregister(EventDispatcherInterface $dispatcher)
8989
*/
9090
public function onKernelException(GetResponseForExceptionEvent $event)
9191
{
92-
$exception = $event->getException();
92+
$exception = $event->getThrowable();
9393
do {
9494
if ($exception instanceof AuthenticationException) {
9595
return $this->handleAuthenticationException($event, $exception);
@@ -111,13 +111,13 @@ private function handleAuthenticationException(GetResponseForExceptionEvent $eve
111111
$event->setResponse($this->startAuthentication($event->getRequest(), $exception));
112112
$event->allowCustomResponseCode();
113113
} catch (\Exception $e) {
114-
$event->setException($e);
114+
$event->setThrowable($e);
115115
}
116116
}
117117

118118
private function handleAccessDeniedException(GetResponseForExceptionEvent $event, AccessDeniedException $exception)
119119
{
120-
$event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception));
120+
$event->setThrowable(new AccessDeniedHttpException($exception->getMessage(), $exception));
121121

122122
$token = $this->tokenStorage->getToken();
123123
if (!$this->authenticationTrustResolver->isFullFledged($token)) {
@@ -131,7 +131,7 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
131131

132132
$event->setResponse($this->startAuthentication($event->getRequest(), $insufficientAuthenticationException));
133133
} catch (\Exception $e) {
134-
$event->setException($e);
134+
$event->setThrowable($e);
135135
}
136136

137137
return;
@@ -160,7 +160,7 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
160160
$this->logger->error('An exception was thrown when handling an AccessDeniedException.', ['exception' => $e]);
161161
}
162162

163-
$event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e));
163+
$event->setThrowable(new \RuntimeException('Exception thrown when handling an exception.', 0, $e));
164164
}
165165
}
166166

src/Symfony/Component/Security/Http/Tests/Firewall/ExceptionListenerTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testAuthenticationExceptionWithoutEntryPoint(\Exception $excepti
4040
$listener->onKernelException($event);
4141

4242
$this->assertNull($event->getResponse());
43-
$this->assertEquals($eventException, $event->getException());
43+
$this->assertEquals($eventException, $event->getThrowable());
4444
}
4545

4646
/**
@@ -59,7 +59,7 @@ public function testAuthenticationExceptionWithEntryPoint(\Exception $exception)
5959

6060
$this->assertEquals('Forbidden', $event->getResponse()->getContent());
6161
$this->assertEquals(403, $event->getResponse()->getStatusCode());
62-
$this->assertSame($exception, $event->getException());
62+
$this->assertSame($exception, $event->getThrowable());
6363
}
6464

6565
public function getAuthenticationExceptionProvider()
@@ -83,8 +83,8 @@ public function testExceptionWhenEntryPointReturnsBadValue()
8383
$listener = $this->createExceptionListener(null, null, null, $entryPoint);
8484
$listener->onKernelException($event);
8585
// the exception has been replaced by our LogicException
86-
$this->assertInstanceOf('LogicException', $event->getException());
87-
$this->assertStringEndsWith('start() method must return a Response object (string returned)', $event->getException()->getMessage());
86+
$this->assertInstanceOf('LogicException', $event->getThrowable());
87+
$this->assertStringEndsWith('start() method must return a Response object (string returned)', $event->getThrowable()->getMessage());
8888
}
8989

9090
/**
@@ -98,7 +98,7 @@ public function testAccessDeniedExceptionFullFledgedAndWithoutAccessDeniedHandle
9898
$listener->onKernelException($event);
9999

100100
$this->assertNull($event->getResponse());
101-
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
101+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
102102
}
103103

104104
/**
@@ -121,7 +121,7 @@ public function testAccessDeniedExceptionFullFledgedAndWithoutAccessDeniedHandle
121121

122122
$this->assertEquals('Unauthorized', $event->getResponse()->getContent());
123123
$this->assertEquals(401, $event->getResponse()->getStatusCode());
124-
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
124+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
125125
}
126126

127127
/**
@@ -138,7 +138,7 @@ public function testAccessDeniedExceptionFullFledgedAndWithAccessDeniedHandlerAn
138138
$listener->onKernelException($event);
139139

140140
$this->assertEquals('error', $event->getResponse()->getContent());
141-
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
141+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
142142
}
143143

144144
/**
@@ -155,7 +155,7 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
155155
$listener->onKernelException($event);
156156

157157
$this->assertEquals('OK', $event->getResponse()->getContent());
158-
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
158+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
159159
}
160160

161161
public function getAccessDeniedExceptionProvider()

0 commit comments

Comments
 (0)