• 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

66.67
/src/Problem/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\Problem\Serializer;
15

16
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
17
use Symfony\Component\ErrorHandler\Exception\FlattenException;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
use Symfony\Component\Serializer\Serializer;
20

21
/**
22
 * Normalizes errors according to the API Problem spec (RFC 7807).
23
 *
24
 * @see https://tools.ietf.org/html/rfc7807
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 */
28
final class ErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
29
{
30
    use ErrorNormalizerTrait;
31
    public const FORMAT = 'jsonproblem';
32
    public const TYPE = 'type';
33
    public const TITLE = 'title';
34
    private array $defaultContext = [
35
        self::TYPE => 'https://tools.ietf.org/html/rfc2616#section-10',
36
        self::TITLE => 'An error occurred',
37
    ];
38

39
    public function __construct(private readonly bool $debug = false, array $defaultContext = [])
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.', __CLASS__));
27✔
50
        $data = [
27✔
51
            'type' => $context[self::TYPE] ?? $this->defaultContext[self::TYPE],
27✔
52
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
27✔
53
            'detail' => $this->getErrorMessage($object, $context, $this->debug),
27✔
54
        ];
27✔
55

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

60
        return $data;
27✔
61
    }
62

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

72
        return self::FORMAT === $format && ($data instanceof \Exception || $data instanceof FlattenException);
3✔
73
    }
74

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

84
        return [];
54✔
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

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

© 2026 Coveralls, Inc