Skip to content

Commit 93983a1

Browse files
authored
Merge pull request #8 from tyx/fix/handle-uncaught-exception
🚑 Reverse the handle of uncaught exception
2 parents 36c29af + cf2d8a0 commit 93983a1

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

JsonExceptionHandler.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function onKernelException(GetResponseForExceptionEvent $event)
2626
$response = null;
2727

2828
if (null !== $resolvedHttpCode) {
29-
$response =new JsonResponse(['errors' => ['message' => $exception->getMessage()]], $resolvedHttpCode);
29+
$response = new JsonResponse(['errors' => ['message' => $exception->getMessage()]], $resolvedHttpCode);
3030
}
3131

3232
if ($exception instanceof InvalidCommandException) {
@@ -56,12 +56,9 @@ public function onKernelException(GetResponseForExceptionEvent $event)
5656
$response = new JsonResponse(['errors' => $violationsPayload], 400);
5757
}
5858

59-
if (null === $response) {
60-
// If no support found, we let JsonExceptionController show it
61-
throw $exception;
59+
if (null !== $response) {
60+
// Will stop the propagation according to symfony doc
61+
$event->setResponse($response);
6262
}
63-
64-
// Will stop the propagation according to symfony doc
65-
$event->setResponse($response);
6663
}
6764
}

tests/Units/JsonExceptionHandler.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66

77
class JsonExceptionHandler extends atoum\test
88
{
9-
public function test_uncaught_exception_should_be_thrown()
9+
public function test_uncaught_exception_should_not_alter_event_response()
1010
{
1111
$this
1212
->given(
1313
$this->newTestedInstance(
1414
new \Rezzza\SymfonyRestApiJson\ExceptionHttpCodeMap
1515
)
1616
)
17-
->exception(function () {
18-
$this->testedInstance->onKernelException($this->dispatchException(new \Exception('boum')));
19-
})
20-
->hasMessage('boum')
21-
->isInstanceOf('Exception')
17+
->when(
18+
$event = $this->dispatchException(new \Exception('boum')),
19+
$this->testedInstance->onKernelException($event)
20+
)
21+
->then
22+
->variable($event->getResponse())
23+
->isNull()
2224
;
2325
}
2426

0 commit comments

Comments
 (0)