Passed
Push — master ( 9da6fd...0354d3 )
by Rafael
05:30
created

DefaultErrorFormatter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 68
ccs 28
cts 31
cp 0.9032
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C format() 0 48 10
A __construct() 0 3 1
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
        if (!isset($formattedError['tracking_id'])) {
47 2
            $originError = $error->getTrace()[0]['args'][0] ?? null;
48
49 2
            $trackingId = null;
50 2
            $errorCode = Response::HTTP_INTERNAL_SERVER_ERROR;
51 2
            if ($originError instanceof \Exception) {
52 2
                if ($originError instanceof ControlledErrorInterface) {
53 1
                    $errorCode = $originError->getCode();
54 1
                    if (!$originError->getMessage() && $this->errorManager->has($errorCode)) {
55 1
                        $formattedError['message'] = $this->errorManager->get($errorCode)->getMessage();
56
                    }
57
58 1
                } elseif ($originError instanceof ClientAware && $originError->isClientSafe()) {
59 1
                    $errorCode = null;
60
                }
61
62 2
                if ($originError instanceof ValidationError) {
63 1
                    $formattedError['constraintViolations'] = $originError->getViolationsArray();
64
                }
65
66 2
                $trackingId = Uuid::createFromData(
67
                    [
68 2
                        'message' => $originError->getMessage(),
69 2
                        'code' => $originError->getCode(),
70 2
                        'file' => $originError->getFile(),
71 2
                        'line' => $originError->getLine(),
72
                    ]
73
                );
74
75
            } elseif ($originError instanceof FieldNode) {
76
                $errorCode = Response::HTTP_BAD_REQUEST;
77
                $trackingId = Uuid::createFromData($formattedError['message'] ?? null);
78
            }
79
80 2
            $formattedError = array_merge(
81
                [
82 2
                    'code' => $errorCode,
83 2
                    'tracking_id' => $trackingId,
84
                ],
85 2
                $formattedError
86
            );
87
        }
88
89 2
        return $formattedError;
90
    }
91
}
92