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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

56.25
/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 ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
17
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
18
use ApiPlatform\State\ApiResource\Error;
19
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
20
use Symfony\Component\ErrorHandler\Exception\FlattenException;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
22
use Symfony\Component\Serializer\Serializer;
23

24
/**
25
 * Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation.
26
 *
27
 * @author Héctor Hurtarte <hectorh30@gmail.com>
28
 */
29
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
30
{
31
    use ErrorNormalizerTrait;
32

33
    public const FORMAT = 'jsonapi';
34
    public const TITLE = 'title';
35
    private array $defaultContext = [
36
        self::TITLE => 'An error occurred',
37
    ];
38

39
    public function __construct(private readonly bool $debug = false, array $defaultContext = [], private ?NormalizerInterface $itemNormalizer = null, private ?NormalizerInterface $constraintViolationListNormalizer = null)
40
    {
41
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
152✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function normalize(mixed $object, string $format = null, array $context = []): array
48
    {
49
        // TODO: in api platform 4 this will be the default, note that JSON:API is close to Problem so we should use the same normalizer
50
        if ($context['rfc_7807_compliant_errors'] ?? false) {
32✔
51
            if ($object instanceof ConstraintViolationListAwareExceptionInterface) {
×
52
                // TODO: return ['errors' => $this->constraintViolationListNormalizer(...)]
53
                return $this->constraintViolationListNormalizer->normalize($object->getConstraintViolationList(), $format, $context);
×
54
            }
55

56
            $jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context);
×
57
            $error = $jsonApiObject['data']['attributes'];
×
58
            $error['id'] = $jsonApiObject['data']['id'];
×
59

60
            return ['errors' => [$error]];
×
61
        }
62

63
        $data = [
32✔
64
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
32✔
65
            'description' => $this->getErrorMessage($object, $context, $this->debug),
32✔
66
        ];
32✔
67

68
        if (null !== $errorCode = $this->getErrorCode($object)) {
32✔
69
            $data['code'] = $errorCode;
8✔
70
        }
71

72
        if ($this->debug && null !== $trace = $object->getTrace()) {
32✔
73
            $data['trace'] = $trace;
12✔
74
        }
75

76
        return $data;
32✔
77
    }
78

79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
83
    {
84
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
4✔
85
    }
86

87
    public function getSupportedTypes($format): array
88
    {
89
        if (self::FORMAT === $format) {
100✔
90
            return [
4✔
91
                \Exception::class => true,
4✔
92
                FlattenException::class => true,
4✔
93
            ];
4✔
94
        }
95

96
        return [];
100✔
97
    }
98

99
    public function hasCacheableSupportsMethod(): bool
100
    {
101
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
102
            trigger_deprecation(
×
103
                'api-platform/core',
×
104
                '3.1',
×
105
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
106
                __METHOD__
×
107
            );
×
108
        }
109

110
        return true;
×
111
    }
112
}
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