Skip to content

Commit 619f90b

Browse files
committed
Working with Throwables instead of Exceptions
1 parent e43442c commit 619f90b

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

AsyncHttpKernel.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
namespace Symfony\Component\HttpKernel;
1717

18-
use Exception;
1918
use React\Promise\FulfilledPromise;
2019
use React\Promise\PromiseInterface;
2120
use React\Promise\RejectedPromise;
@@ -38,6 +37,7 @@
3837
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
3938
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
4039
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
40+
use Throwable;
4141

4242
/**
4343
* Class AsyncHttpKernel.
@@ -115,18 +115,18 @@ public function handleAsync(
115115
$type
116116
)
117117
->then(null,
118-
function (\Exception $e) use ($request, $type, $catch) {
119-
if ($e instanceof RequestExceptionInterface) {
120-
$e = new BadRequestHttpException($e->getMessage(), $e);
118+
function (Throwable $exception) use ($request, $type, $catch) {
119+
if ($exception instanceof RequestExceptionInterface) {
120+
$exception = new BadRequestHttpException($exception->getMessage(), $exception);
121121
}
122122

123123
if (false === $catch) {
124124
$this->finishRequestPromise($request, $type);
125125

126-
throw $e;
126+
throw $exception;
127127
}
128128

129-
return $this->handleExceptionPromise($e, $request, $type);
129+
return $this->handleExceptionPromise($exception, $request, $type);
130130
}
131131
);
132132
}
@@ -269,20 +269,20 @@ private function finishRequestPromise(Request $request, int $type)
269269
/**
270270
* Handles an exception by trying to convert it to a Response.
271271
*
272-
* @param \Exception $e An \Exception instance
273-
* @param Request $request A Request instance
274-
* @param int $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
272+
* @param Throwable $exception
273+
* @param Request $request
274+
* @param int $type
275275
*
276276
* @return PromiseInterface
277277
*
278-
* @throws \Exception
278+
* @throws \Throwable
279279
*/
280280
private function handleExceptionPromise(
281-
Exception $e,
281+
Throwable $exception,
282282
Request $request,
283283
int $type
284284
): PromiseInterface {
285-
$event = new GetResponsePromiseForExceptionEvent($this, $request, $type, $e);
285+
$event = new GetResponsePromiseForExceptionEvent($this, $request, $type, $exception);
286286
$promise = $this
287287
->dispatcher
288288
->asyncDispatch(AsyncKernelEvents::ASYNC_EXCEPTION, $event)

AsyncKernel.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ public function process(ContainerBuilder $container)
7777

7878
$loop = new Definition(LoopInterface::class);
7979
$loop->setSynthetic(true);
80-
80+
8181
$container->setDefinition('reactphp.event_loop', $loop);
8282
$container->setAlias(LoopInterface::class, 'reactphp.event_loop');
83-
8483
}
8584
}

Event/GetResponsePromiseForExceptionEvent.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515

1616
namespace Symfony\Component\HttpKernel\Event;
1717

18-
use Exception;
1918
use Symfony\Component\HttpFoundation\Request;
2019
use Symfony\Component\HttpKernel\HttpKernelInterface;
20+
use Throwable;
2121

2222
/**
2323
* Class GetResponsePromiseForExceptionEvent.
@@ -27,7 +27,7 @@ class GetResponsePromiseForExceptionEvent extends GetResponsePromiseEvent
2727
/**
2828
* The exception object.
2929
*
30-
* @var \Exception
30+
* @var Throwable
3131
*/
3232
private $exception;
3333

@@ -42,13 +42,13 @@ class GetResponsePromiseForExceptionEvent extends GetResponsePromiseEvent
4242
* @param HttpKernelInterface $kernel
4343
* @param Request $request
4444
* @param int $requestType
45-
* @param Exception $exception
45+
* @param Throwable $exception
4646
*/
4747
public function __construct(
4848
HttpKernelInterface $kernel,
4949
Request $request,
5050
int $requestType,
51-
Exception $exception
51+
Throwable $exception
5252
) {
5353
parent::__construct($kernel, $request, $requestType);
5454

@@ -58,9 +58,9 @@ public function __construct(
5858
/**
5959
* Returns the thrown exception.
6060
*
61-
* @return \Exception The thrown exception
61+
* @return Throwable
6262
*/
63-
public function getException()
63+
public function getException(): Throwable
6464
{
6565
return $this->exception;
6666
}
@@ -70,9 +70,9 @@ public function getException()
7070
*
7171
* This exception will be thrown if no response is set in the event.
7272
*
73-
* @param \Exception $exception The thrown exception
73+
* @param Throwable $exception
7474
*/
75-
public function setException(\Exception $exception)
75+
public function setException(Throwable $exception)
7676
{
7777
$this->exception = $exception;
7878
}

0 commit comments

Comments
 (0)