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

api-platform / core / 9710836697

28 Jun 2024 09:35AM UTC coverage: 63.285% (+1.2%) from 62.122%
9710836697

push

github

soyuka
docs: changelog v3.3.7

11104 of 17546 relevant lines covered (63.29%)

52.26 hits per line

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

54.55
/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\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface;
19
use ApiPlatform\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);
295✔
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 LegacyConstraintViolationListAwareExceptionInterface || $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
            $error['type'] = $jsonApiObject['data']['id'];
×
60

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

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

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

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

77
        return $data;
32✔
78
    }
79

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

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

97
        return [];
215✔
98
    }
99

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

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

© 2025 Coveralls, Inc