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

api-platform / core / 6978770879

24 Nov 2023 09:02AM UTC coverage: 37.284% (-0.1%) from 37.409%
6978770879

push

github

soyuka
Merge 3.2

79 of 149 new or added lines in 21 files covered. (53.02%)

16 existing lines in 8 files now uncovered.

10287 of 27591 relevant lines covered (37.28%)

20.53 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);
114✔
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) {
24✔
NEW
51
            if ($object instanceof ConstraintViolationListAwareExceptionInterface) {
×
52
                // TODO: return ['errors' => $this->constraintViolationListNormalizer(...)]
NEW
53
                return $this->constraintViolationListNormalizer->normalize($object->getConstraintViolationList(), $format, $context);
×
54
            }
55

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

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

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

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

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

76
        return $data;
24✔
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);
3✔
85
    }
86

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

96
        return [];
79✔
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

NEW
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

© 2025 Coveralls, Inc