Skip to content

Commit a209dd4

Browse files
authored
fix: add missing error normalizer trait and remove deprecated interface (#6853)
1 parent 978975e commit a209dd4

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

Diff for: src/JsonApi/Serializer/ErrorNormalizer.php

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace ApiPlatform\JsonApi\Serializer;
1515

16-
use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
1716
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
1817
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface;
1918
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;

Diff for: src/JsonApi/Serializer/ErrorNormalizerTrait.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\JsonApi\Serializer;
15+
16+
use Symfony\Component\ErrorHandler\Exception\FlattenException;
17+
use Symfony\Component\HttpFoundation\Response;
18+
19+
trait ErrorNormalizerTrait
20+
{
21+
private function getErrorMessage($object, array $context, bool $debug = false): string
22+
{
23+
$message = $object->getMessage();
24+
25+
if ($debug) {
26+
return $message;
27+
}
28+
29+
if ($object instanceof FlattenException) {
30+
$statusCode = $context['statusCode'] ?? $object->getStatusCode();
31+
if ($statusCode >= 500 && $statusCode < 600) {
32+
$message = Response::$statusTexts[$statusCode] ?? Response::$statusTexts[Response::HTTP_INTERNAL_SERVER_ERROR];
33+
}
34+
}
35+
36+
return $message;
37+
}
38+
39+
private function getErrorCode(object $object): ?string
40+
{
41+
if ($object instanceof FlattenException) {
42+
return (string)$object->getStatusCode();
43+
}
44+
45+
if ($object instanceof \Exception) {
46+
$code = $object->getCode();
47+
return $code !== 0 ? (string)$code : null;
48+
}
49+
50+
return null;
51+
}
52+
}
53+

0 commit comments

Comments
 (0)