66
77use Phpro \ApiProblem \ApiProblemInterface ;
88use Phpro \ApiProblem \DebuggableApiProblemInterface ;
9- use Phpro \ApiProblem \Exception \ApiProblemException ;
109use Phpro \ApiProblem \Http \ExceptionApiProblem ;
10+ use Phpro \ApiProblemBundle \Transformer \ExceptionTransformerInterface ;
1111use Symfony \Component \HttpFoundation \JsonResponse ;
1212use Symfony \Component \HttpFoundation \Response ;
1313use Symfony \Component \HttpKernel \Event \GetResponseForExceptionEvent ;
1414
1515class JsonApiProblemExceptionListener
1616{
17+ /**
18+ * @var ExceptionTransformerInterface
19+ */
20+ private $ exceptionTransformer ;
21+
1722 /**
1823 * @var bool
1924 */
2025 private $ debug ;
2126
22- public function __construct (bool $ debug )
27+ public function __construct (ExceptionTransformerInterface $ exceptionTransformer , bool $ debug )
2328 {
29+ $ this ->exceptionTransformer = $ exceptionTransformer ;
2430 $ this ->debug = $ debug ;
2531 }
2632
@@ -40,11 +46,11 @@ public function onKernelException(GetResponseForExceptionEvent $event): void
4046
4147 private function convertExceptionToProblem (\Throwable $ exception ): ApiProblemInterface
4248 {
43- if ($ exception instanceof ApiProblemException ) {
44- return $ exception-> getApiProblem ( );
49+ if (! $ this -> exceptionTransformer -> accepts ( $ exception) ) {
50+ return new ExceptionApiProblem ( $ exception );
4551 }
4652
47- return new ExceptionApiProblem ($ exception );
53+ return $ this -> exceptionTransformer -> transform ($ exception );
4854 }
4955
5056 private function generateResponse (ApiProblemInterface $ apiProblem ): JsonResponse
0 commit comments