@@ -25,7 +25,7 @@ final class ErrorNormalizer implements NormalizerInterface
2525{
2626 public const FORMAT = 'jsonapi ' ;
2727
28- public function __construct (private ?NormalizerInterface $ itemNormalizer = null )
28+ public function __construct (private ?NormalizerInterface $ itemNormalizer = null , private ? NormalizerInterface $ problemNormalizer = null )
2929 {
3030 }
3131
@@ -35,7 +35,7 @@ public function __construct(private ?NormalizerInterface $itemNormalizer = null)
3535 public function normalize (mixed $ data , ?string $ format = null , array $ context = []): array
3636 {
3737 if ($ data instanceof FlattenException) {
38- $ error = $ this ->flattenExceptionToError ($ data , $ context );
38+ $ error = $ this ->flattenExceptionToError ($ data , $ format , $ context );
3939 } else {
4040 $ jsonApiObject = $ this ->itemNormalizer ->normalize ($ data , $ format , $ context );
4141 $ error = $ jsonApiObject ['data ' ]['attributes ' ] ?? [];
@@ -77,17 +77,19 @@ public function normalize(mixed $data, ?string $format = null, array $context =
7777 return ['errors ' => $ errors ];
7878 }
7979
80- private function flattenExceptionToError (FlattenException $ exception , array $ context ): array
80+ private function flattenExceptionToError (FlattenException $ exception , ? string $ format , array $ context ): array
8181 {
82- $ status = $ exception ->getStatusCode ();
82+ $ type = \sprintf ('/errors/%d ' , $ exception ->getStatusCode ());
83+ unset($ context ['exception ' ]);
84+ $ problem = $ this ->problemNormalizer ->normalize ($ exception , $ format , ['type ' => $ type ] + $ context );
8385
8486 return [
85- 'id ' => " /errors/ $ status " ,
86- 'title ' => ' An error occurred ' ,
87- 'detail ' => ( $ context [ ' debug ' ] ?? false ) ? $ exception -> getMessage () : $ exception -> getStatusText () ,
88- 'status ' => $ status ,
89- 'type ' => " /errors/ $ status " ,
90- 'code ' => (string ) $ status ,
87+ 'id ' => $ problem [ ' type ' ] ,
88+ 'title ' => $ problem [ ' title ' ] ,
89+ 'detail ' => $ problem [ ' detail ' ] ,
90+ 'status ' => $ problem [ ' status ' ] ,
91+ 'type ' => $ problem [ ' type ' ] ,
92+ 'code ' => (string ) $ problem [ ' status ' ] ,
9193 ];
9294 }
9395
@@ -96,7 +98,7 @@ private function flattenExceptionToError(FlattenException $exception, array $con
9698 */
9799 public function supportsNormalization (mixed $ data , ?string $ format = null , array $ context = []): bool
98100 {
99- return self ::FORMAT === $ format && ($ data instanceof \Exception || $ data instanceof FlattenException);
101+ return self ::FORMAT === $ format && ($ data instanceof \Exception || ( $ data instanceof FlattenException && null !== $ this -> problemNormalizer ) );
100102 }
101103
102104 /**
@@ -107,7 +109,7 @@ public function getSupportedTypes(?string $format): array
107109 if (self ::FORMAT === $ format ) {
108110 return [
109111 \Exception::class => true ,
110- FlattenException::class => true ,
112+ FlattenException::class => null !== $ this -> problemNormalizer ,
111113 ];
112114 }
113115
0 commit comments