Skip to content

Commit 3eace8a

Browse files
committed
fix(jsonapi): normalize flatten exceptions through the problem normalizer
1 parent 348a297 commit 3eace8a

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

‎src/JsonApi/Serializer/ErrorNormalizer.php‎

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

‎src/Symfony/Bundle/Resources/config/jsonapi.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,6 @@
119119
->tag('serializer.normalizer', ['priority' => -780]);
120120

121121
$services->set('api_platform.jsonapi.normalizer.error', ErrorNormalizer::class)
122-
->args([service('api_platform.jsonapi.normalizer.item')])
122+
->args([service('api_platform.jsonapi.normalizer.item'), service('serializer.normalizer.problem')->nullOnInvalid()])
123123
->tag('serializer.normalizer', ['priority' => -790]);
124124
};

0 commit comments

Comments
 (0)