• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 20519317585

26 Dec 2025 08:38AM UTC coverage: 25.193% (-0.01%) from 25.206%
20519317585

push

github

web-flow
fix(test): change string to int for status in JsonApiTest::testError (#7631)

14623 of 58043 relevant lines covered (25.19%)

29.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.62
/src/JsonApi/Serializer/ErrorNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\Serializer\Normalizer\NormalizerInterface;
18

19
/**
20
 * Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation.
21
 *
22
 * @author Héctor Hurtarte <hectorh30@gmail.com>
23
 */
24
final class ErrorNormalizer implements NormalizerInterface
25
{
26
    public const FORMAT = 'jsonapi';
27

28
    public function __construct(private ?NormalizerInterface $itemNormalizer = null)
29
    {
30
    }
772✔
31

32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
36
    {
37
        $jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context);
6✔
38
        $error = $jsonApiObject['data']['attributes'] ?? [];
6✔
39
        $error['id'] = $jsonApiObject['data']['id'];
6✔
40
        if (isset($error['type'])) {
6✔
41
            $error['links'] = ['type' => $error['type']];
6✔
42
        }
43

44
        if (!isset($error['code']) && method_exists($object, 'getId')) {
6✔
45
            $error['code'] = $object->getId();
6✔
46
        }
47

48
        // TODO: change this 5.x
49
        // if (isset($error['status'])) {
50
        //     $error['status'] = (string) $error['status'];
51
        // }
52

53
        if (!isset($error['violations'])) {
6✔
54
            return ['errors' => [$error]];
6✔
55
        }
56

57
        $errors = [];
×
58
        foreach ($error['violations'] as $violation) {
×
59
            $e = ['detail' => $violation['message']] + $error;
×
60
            if (isset($error['links']['type'])) {
×
61
                $type = $error['links']['type'];
×
62
                $e['links']['type'] = \sprintf('%s/%s', $type, $violation['propertyPath']);
×
63
                $e['id'] = str_replace($type, $e['links']['type'], $e['id']);
×
64
            }
65
            if (isset($e['code'])) {
×
66
                $e['code'] = \sprintf('%s/%s', $error['code'], $violation['propertyPath']);
×
67
            }
68
            unset($e['violations']);
×
69
            $errors[] = $e;
×
70
        }
71

72
        return ['errors' => $errors];
×
73
    }
74

75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
79
    {
80
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
6✔
81
    }
82

83
    /**
84
     * @param string|null $format
85
     */
86
    public function getSupportedTypes($format): array
87
    {
88
        if (self::FORMAT === $format) {
638✔
89
            return [
36✔
90
                \Exception::class => true,
36✔
91
                FlattenException::class => true,
36✔
92
            ];
36✔
93
        }
94

95
        return [];
602✔
96
    }
97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc