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

HttpExceptionNormalizer::normalize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 10
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\HttpKernel\Exception\HttpException;
19
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
20
21
/**
22
 * Normalize HTTP exceptions.
23
 *
24
 * @experimental
25
 *
26
 * @author Alan Poulain <[email protected]>
27
 */
28
final class HttpExceptionNormalizer implements NormalizerInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function normalize($object, $format = null, array $context = []): array
34
    {
35
        /** @var HttpException */
36
        $httpException = $object->getPrevious();
37
        $error = FormattedError::createFromException($object);
38
        $error['message'] = $httpException->getMessage();
39
        $error['extensions']['status'] = $statusCode = $httpException->getStatusCode();
40
        $error['extensions']['category'] = $statusCode < 500 ? 'user' : Error::CATEGORY_INTERNAL;
41
42
        return $error;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function supportsNormalization($data, $format = null): bool
49
    {
50
        return $data instanceof Error && $data->getPrevious() instanceof HttpException;
51
    }
52
}
53