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

api-platform / core / 6665644319

27 Oct 2023 09:57AM UTC coverage: 37.409% (+0.1%) from 37.305%
6665644319

push

github

soyuka
Merge 3.2

125 of 125 new or added lines in 32 files covered. (100.0%)

10319 of 27584 relevant lines covered (37.41%)

20.57 hits per line

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

66.67
/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 Symfony\Component\ErrorHandler\Exception\FlattenException;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Serializer\Serializer;
21

22
/**
23
 * Converts {@see \Exception} or {@see FlattenException} or to a JSON API error representation.
24
 *
25
 * @deprecated we use ItemNormalizer instead
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 readonly ?NormalizerInterface $itemNormalizer = null)
40
    {
41
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
111✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function normalize(mixed $object, string $format = null, array $context = []): array
48
    {
49
        trigger_deprecation('api-platform', '3.2', sprintf('The class "%s" is deprecated in favor of using an Error resource. We fallback on "api_platform.serializer.normalizer.item".', __CLASS__));
24✔
50

51
        if ($this->itemNormalizer) {
24✔
52
            return $this->itemNormalizer->normalize($object, $format, $context);
×
53
        }
54

55
        $data = [
24✔
56
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
24✔
57
            'description' => $this->getErrorMessage($object, $context, $this->debug),
24✔
58
        ];
24✔
59

60
        if (null !== $errorCode = $this->getErrorCode($object)) {
24✔
61
            $data['code'] = $errorCode;
6✔
62
        }
63

64
        if ($this->debug && null !== $trace = $object->getTrace()) {
24✔
65
            $data['trace'] = $trace;
9✔
66
        }
67

68
        return $data;
24✔
69
    }
70

71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
75
    {
76
        if ($context['skip_deprecated_exception_normalizers'] ?? false) {
3✔
77
            return false;
×
78
        }
79

80
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
3✔
81
    }
82

83
    public function getSupportedTypes($format): array
84
    {
85
        if (self::FORMAT === $format) {
76✔
86
            return [
3✔
87
                \Exception::class => false,
3✔
88
                FlattenException::class => false,
3✔
89
            ];
3✔
90
        }
91

92
        return [];
76✔
93
    }
94

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

106
        return false;
×
107
    }
108
}
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