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

api-platform / core / 12314371934

13 Dec 2024 10:57AM UTC coverage: 62.118% (-0.003%) from 62.121%
12314371934

push

github

soyuka
ci: use problem error normalizer trait

11493 of 18502 relevant lines covered (62.12%)

68.69 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\Serializer\CacheableSupportsMethodInterface;
17
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as LegacyConstraintViolationListAwareExceptionInterface;
18
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
19
use Symfony\Component\ErrorHandler\Exception\FlattenException;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
use Symfony\Component\Serializer\Serializer;
22

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

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

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

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
47
    {
48
        // 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
49
        if ($context['rfc_7807_compliant_errors'] ?? false) {
32✔
50
            if ($object instanceof LegacyConstraintViolationListAwareExceptionInterface || $object instanceof ConstraintViolationListAwareExceptionInterface) {
×
51
                // TODO: return ['errors' => $this->constraintViolationListNormalizer(...)]
52
                return $this->constraintViolationListNormalizer->normalize($object->getConstraintViolationList(), $format, $context);
×
53
            }
54

55
            $jsonApiObject = $this->itemNormalizer->normalize($object, $format, $context);
×
56
            $error = $jsonApiObject['data']['attributes'];
×
57
            $error['id'] = $jsonApiObject['data']['id'];
×
58
            $error['type'] = $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) {
447✔
90
            return [
60✔
91
                \Exception::class => true,
60✔
92
                FlattenException::class => true,
60✔
93
            ];
60✔
94
        }
95

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

© 2025 Coveralls, Inc