Completed
Push — master ( 179b7b...4a62fb )
by Rafael
08:47
created

DefaultErrorFormatter::format()   C

Complexity

Conditions 9
Paths 10

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 9.111

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 24
cts 27
cp 0.8889
rs 5.0942
c 0
b 0
f 0
cc 9
eloc 26
nc 10
nop 2
crap 9.111
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Error;
12
13
use GraphQL\Error\ClientAware;
14
use GraphQL\Error\Error;
15
use GraphQL\Error\FormattedError;
16
use GraphQL\Language\AST\FieldNode;
17
use Symfony\Component\HttpFoundation\Response;
18
use Ynlo\GraphQLBundle\Exception\Controlled\ValidationError;
19
use Ynlo\GraphQLBundle\Exception\ControlledErrorInterface;
20
use Ynlo\GraphQLBundle\Util\Uuid;
21
22
class DefaultErrorFormatter implements ErrorFormatterInterface
23
{
24
    /**
25
     * @var ControlledErrorManager
26
     */
27
    protected $errorManager;
28
29
    /**
30
     * DefaultErrorFormatter constructor.
31
     *
32
     * @param ControlledErrorManager $errorManager
33
     */
34 22
    public function __construct(ControlledErrorManager $errorManager)
35
    {
36 22
        $this->errorManager = $errorManager;
37 22
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 2
    public function format(Error $error, $debug = false): array
43
    {
44 2
        $formattedError = FormattedError::createFromException($error, $debug);
45
46 2
        $originError = $error->getTrace()[0]['args'][0] ?? null;
47
48 2
        $trackingId = null;
49 2
        $errorCode = Response::HTTP_INTERNAL_SERVER_ERROR;
50 2
        if ($originError instanceof \Throwable) {
51 2
            if ($originError instanceof ControlledErrorInterface) {
52 1
                $errorCode = $originError->getCode();
53 1
                if (!$originError->getMessage() && $this->errorManager->has($errorCode)) {
54 1
                    $formattedError['message'] = $this->errorManager->get($errorCode)->getMessage();
55
                }
56
57 1
            } elseif ($originError instanceof ClientAware && $originError->isClientSafe()) {
58 1
                $errorCode = null;
59
            }
60
61 2
            if ($originError instanceof ValidationError) {
62 1
                $formattedError['constraintViolations'] = $originError->getViolationsArray();
63
            }
64
65 2
            $trackingId = Uuid::createFromData(
66
                [
67 2
                    'message' => $originError->getMessage(),
68 2
                    'code' => $originError->getCode(),
69 2
                    'file' => $originError->getFile(),
70 2
                    'line' => $originError->getLine(),
71
                ]
72
            );
73
74
        } elseif ($originError instanceof FieldNode) {
75
            $errorCode = Response::HTTP_BAD_REQUEST;
76
            $trackingId = Uuid::createFromData($formattedError['message'] ?? null);
77
        }
78
79 2
        $formattedError = array_merge(
80
            [
81 2
                'code' => $errorCode,
82 2
                'tracking_id' => $trackingId,
83
            ],
84 2
            $formattedError
85
        );
86
87 2
        return $formattedError;
88
    }
89
}
90