• 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

28.57
/src/Hydra/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\Hydra\Serializer;
15

16
use ApiPlatform\Api\UrlGeneratorInterface;
17
use ApiPlatform\Problem\Serializer\ErrorNormalizerTrait;
18
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
19
use ApiPlatform\State\ApiResource\Error;
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} to a Hydra error representation.
26
 *
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 * @author Samuel ROZE <samuel.roze@gmail.com>
29
 */
30
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
31
{
32
    use ErrorNormalizerTrait;
33

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

38
    public function __construct(private readonly UrlGeneratorInterface $urlGenerator, private readonly bool $debug = false, array $defaultContext = [])
39
    {
40
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
87✔
41
    }
42

43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
47
    {
48
        $data = [
×
UNCOV
49
            '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'Error']),
×
50
            '@type' => 'hydra:Error',
×
51
            'hydra:title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
×
52
            'hydra:description' => $this->getErrorMessage($object, $context, $this->debug),
×
53
        ];
×
54

55
        if ($this->debug && null !== $trace = $object->getTrace()) {
×
56
            $data['trace'] = $trace;
×
57
        }
58

59
        return $data;
×
60
    }
61

62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
66
    {
NEW
67
        if ($context['api_error_resource'] ?? false) {
×
68
            return false;
×
69
        }
70

NEW
71
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
×
72
    }
73

74
    public function getSupportedTypes($format): array
75
    {
76
        if (self::FORMAT === $format) {
57✔
77
            return [
33✔
78
                \Exception::class => true,
33✔
79
                Error::class => false,
33✔
80
                FlattenException::class => true,
33✔
81
            ];
33✔
82
        }
83

84
        return [];
24✔
85
    }
86

87
    public function hasCacheableSupportsMethod(): bool
88
    {
89
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
90
            trigger_deprecation(
×
91
                'api-platform/core',
×
92
                '3.1',
×
93
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
94
                __METHOD__
×
95
            );
×
96
        }
97

NEW
98
        return true;
×
99
    }
100
}
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