Skip to content

Commit 4785fe8

Browse files
[HttpKernel] make ExceptionEvent able to propagate any throwable
1 parent 30652fa commit 4785fe8

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

Firewall/ExceptionListener.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function unregister(EventDispatcherInterface $dispatcher)
9090
*/
9191
public function onKernelException(GetResponseForExceptionEvent $event)
9292
{
93-
$exception = $event->getException();
93+
$exception = $event->getThrowable();
9494
do {
9595
if ($exception instanceof AuthenticationException) {
9696
$this->handleAuthenticationException($event, $exception);
@@ -128,13 +128,13 @@ private function handleAuthenticationException(GetResponseForExceptionEvent $eve
128128
$event->setResponse($this->startAuthentication($event->getRequest(), $exception));
129129
$event->allowCustomResponseCode();
130130
} catch (\Exception $e) {
131-
$event->setException($e);
131+
$event->setThrowable($e);
132132
}
133133
}
134134

135135
private function handleAccessDeniedException(GetResponseForExceptionEvent $event, AccessDeniedException $exception)
136136
{
137-
$event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception));
137+
$event->setThrowable(new AccessDeniedHttpException($exception->getMessage(), $exception));
138138

139139
$token = $this->tokenStorage->getToken();
140140
if (!$this->authenticationTrustResolver->isFullFledged($token)) {
@@ -148,7 +148,7 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
148148

149149
$event->setResponse($this->startAuthentication($event->getRequest(), $insufficientAuthenticationException));
150150
} catch (\Exception $e) {
151-
$event->setException($e);
151+
$event->setThrowable($e);
152152
}
153153

154154
return;
@@ -177,7 +177,7 @@ private function handleAccessDeniedException(GetResponseForExceptionEvent $event
177177
$this->logger->error('An exception was thrown when handling an AccessDeniedException.', ['exception' => $e]);
178178
}
179179

180-
$event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e));
180+
$event->setThrowable(new \RuntimeException('Exception thrown when handling an exception.', 0, $e));
181181
}
182182
}
183183

Tests/Firewall/ExceptionListenerTest.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
1717
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
18-
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
1918
use Symfony\Component\HttpKernel\Exception\HttpException;
2019
use Symfony\Component\HttpKernel\HttpKernelInterface;
2120
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
@@ -40,7 +39,7 @@ public function testAuthenticationExceptionWithoutEntryPoint(\Exception $excepti
4039
$listener->onKernelException($event);
4140

4241
$this->assertNull($event->getResponse());
43-
$this->assertEquals($eventException, $event->getException());
42+
$this->assertEquals($eventException, $event->getThrowable());
4443
}
4544

4645
/**
@@ -59,7 +58,7 @@ public function testAuthenticationExceptionWithEntryPoint(\Exception $exception)
5958

6059
$this->assertEquals('Forbidden', $event->getResponse()->getContent());
6160
$this->assertEquals(403, $event->getResponse()->getStatusCode());
62-
$this->assertSame($exception, $event->getException());
61+
$this->assertSame($exception, $event->getThrowable());
6362
}
6463

6564
public function getAuthenticationExceptionProvider()
@@ -86,8 +85,8 @@ public function testExceptionWhenEntryPointReturnsBadValue()
8685
$listener = $this->createExceptionListener(null, null, null, $entryPoint);
8786
$listener->onKernelException($event);
8887
// the exception has been replaced by our LogicException
89-
$this->assertInstanceOf('LogicException', $event->getException());
90-
$this->assertStringEndsWith('start() method must return a Response object (string returned)', $event->getException()->getMessage());
88+
$this->assertInstanceOf('LogicException', $event->getThrowable());
89+
$this->assertStringEndsWith('start() method must return a Response object (string returned)', $event->getThrowable()->getMessage());
9190
}
9291

9392
/**
@@ -101,7 +100,7 @@ public function testAccessDeniedExceptionFullFledgedAndWithoutAccessDeniedHandle
101100
$listener->onKernelException($event);
102101

103102
$this->assertNull($event->getResponse());
104-
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
103+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
105104
}
106105

107106
/**
@@ -124,7 +123,7 @@ public function testAccessDeniedExceptionFullFledgedAndWithoutAccessDeniedHandle
124123

125124
$this->assertEquals('Unauthorized', $event->getResponse()->getContent());
126125
$this->assertEquals(401, $event->getResponse()->getStatusCode());
127-
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
126+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
128127
}
129128

130129
/**
@@ -141,7 +140,7 @@ public function testAccessDeniedExceptionFullFledgedAndWithAccessDeniedHandlerAn
141140
$listener->onKernelException($event);
142141

143142
$this->assertEquals('error', $event->getResponse()->getContent());
144-
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
143+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
145144
}
146145

147146
/**
@@ -158,7 +157,7 @@ public function testAccessDeniedExceptionNotFullFledged(\Exception $exception, \
158157
$listener->onKernelException($event);
159158

160159
$this->assertEquals('OK', $event->getResponse()->getContent());
161-
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getException()->getPrevious());
160+
$this->assertSame(null === $eventException ? $exception : $eventException, $event->getThrowable()->getPrevious());
162161
}
163162

164163
public function getAccessDeniedExceptionProvider()
@@ -194,11 +193,7 @@ private function createEvent(\Exception $exception, $kernel = null)
194193
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
195194
}
196195

197-
if (class_exists(ExceptionEvent::class)) {
198-
return new ExceptionEvent($kernel, Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $exception);
199-
}
200-
201-
return new GetResponseForExceptionEvent($kernel, Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $exception);
196+
return new ExceptionEvent($kernel, Request::create('/'), HttpKernelInterface::MASTER_REQUEST, $exception);
202197
}
203198

204199
private function createExceptionListener(TokenStorageInterface $tokenStorage = null, AuthenticationTrustResolverInterface $trustResolver = null, HttpUtils $httpUtils = null, AuthenticationEntryPointInterface $authenticationEntryPoint = null, $errorPage = null, AccessDeniedHandlerInterface $accessDeniedHandler = null)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": "^7.1.3",
2020
"symfony/security-core": "^4.4",
2121
"symfony/http-foundation": "^3.4|^4.0|^5.0",
22-
"symfony/http-kernel": "^4.3",
22+
"symfony/http-kernel": "^4.4",
2323
"symfony/property-access": "^3.4|^4.0|^5.0"
2424
},
2525
"require-dev": {

0 commit comments

Comments
 (0)