• 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

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 ApiPlatform\State\ApiResource\Error;
18
use Symfony\Component\ErrorHandler\Exception\FlattenException;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
use Symfony\Component\Serializer\Serializer;
21

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

40
    public function __construct(private readonly bool $debug = false, array $defaultContext = [])
41
    {
42
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
117✔
43
    }
44

45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function normalize(mixed $object, string $format = null, array $context = []): array
49
    {
50
        $data = [
30✔
51
            'type' => $context[self::TYPE] ?? $this->defaultContext[self::TYPE],
30✔
52
            'title' => $context[self::TITLE] ?? $this->defaultContext[self::TITLE],
30✔
53
            'detail' => $this->getErrorMessage($object, $context, $this->debug),
30✔
54
        ];
30✔
55

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

60
        return $data;
30✔
61
    }
62

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

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

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

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

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

© 2025 Coveralls, Inc