• 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

67.86
/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 Symfony\Component\ErrorHandler\Exception\FlattenException;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Serializer\Serializer;
21

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

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

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

42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function normalize(mixed $object, string $format = null, array $context = []): array
46
    {
47
        trigger_deprecation('api-platform', '3.2', sprintf('The class "%s" is deprecated in favor of using an Error resource.', __CLASS__));
24✔
48
        $data = [
24✔
49
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
24✔
50
            'description' => $this->getErrorMessage($object, $context, $this->debug),
24✔
51
        ];
24✔
52

53
        if (null !== $errorCode = $this->getErrorCode($object)) {
24✔
54
            $data['code'] = $errorCode;
6✔
55
        }
56

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

61
        return $data;
24✔
62
    }
63

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

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

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

85
        return [];
75✔
86
    }
87

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

99
        return false;
×
100
    }
101
}
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