Passed
Push — master ( ce7fff...9920f1 )
by Alan
04:04
created

RuntimeExceptionNormalizer::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
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\Core\GraphQl\Serializer\Exception;
15
16
use GraphQL\Error\Error;
17
use GraphQL\Error\FormattedError;
18
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
19
20
/**
21
 * Normalize runtime exceptions to have the right message in production mode.
22
 *
23
 * @experimental
24
 *
25
 * @author Alan Poulain <[email protected]>
26
 */
27
final class RuntimeExceptionNormalizer implements NormalizerInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function normalize($object, $format = null, array $context = []): array
33
    {
34
        /** @var \RuntimeException */
35
        $runtimeException = $object->getPrevious();
36
        $error = FormattedError::createFromException($object);
37
        $error['message'] = $runtimeException->getMessage();
38
39
        return $error;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function supportsNormalization($data, $format = null): bool
46
    {
47
        return $data instanceof Error && $data->getPrevious() instanceof \RuntimeException;
48
    }
49
}
50