Skip to content

Commit f871400

Browse files
committed
🚑 Reverse the handle of uncaught exception
1 parent 36c29af commit f871400

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

JsonExceptionHandler.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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)