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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

25.0
/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 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} to a Hydra error representation.
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 * @author Samuel ROZE <samuel.roze@gmail.com>
28
 */
29
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
30
{
31
    use ErrorNormalizerTrait;
32

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

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

42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
46
    {
47
        trigger_deprecation('api-platform', '3.2', sprintf('The class "%s" is deprecated in favor of using an Error resource.', __CLASS__));
×
48
        $data = [
×
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
    {
67
        if ($context['skip_deprecated_exception_normalizers'] ?? false) {
×
68
            return false;
×
69
        }
70

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) {
51✔
77
            return [
33✔
78
                \Exception::class => false,
33✔
79
                FlattenException::class => false,
33✔
80
            ];
33✔
81
        }
82

83
        return [];
18✔
84
    }
85

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

97
        return false;
×
98
    }
99
}
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

© 2026 Coveralls, Inc