Completed
Push — master ( 5265ec...af88ae )
by Rafael
03:09
created

DefaultErrorFormatter::format()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
eloc 30
dl 0
loc 50
ccs 0
cts 42
cp 0
rs 6.9666
c 0
b 0
f 0
cc 12
nc 12
nop 2
crap 156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 GraphQL\Type\Schema;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Exception\HttpException;
20
use Ynlo\GraphQLBundle\Exception\Controlled\ValidationError;
21
use Ynlo\GraphQLBundle\Exception\ControlledErrorInterface;
22
use Ynlo\GraphQLBundle\Util\Uuid;
23
24
class DefaultErrorFormatter implements ErrorFormatterInterface
25
{
26
    /**
27
     * @var ControlledErrorManager
28
     */
29
    protected $errorManager;
30
31
    /**
32
     * DefaultErrorFormatter constructor.
33
     *
34
     * @param ControlledErrorManager $errorManager
35
     */
36
    public function __construct(ControlledErrorManager $errorManager)
37
    {
38
        $this->errorManager = $errorManager;
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function format(Error $error, $debug = false): array
45
    {
46
        $formattedError = FormattedError::createFromException($error, $debug);
47
48
        $originError = $error->getTrace()[0]['args'][0] ?? null;
49
50
        $trackingId = null;
51
        $errorCode = Response::HTTP_INTERNAL_SERVER_ERROR;
52
        if ($originError instanceof \Throwable) {
53
            if ($originError instanceof ControlledErrorInterface) {
54
                $errorCode = $originError->getCode();
55
                if (!$originError->getMessage() && $this->errorManager->has($errorCode)) {
56
                    $formattedError['message'] = $this->errorManager->get($errorCode)->getMessage();
57
                }
58
59
            } elseif ($originError instanceof ClientAware && $originError->isClientSafe()) {
60
                $errorCode = 400;
61
            } elseif ($originError instanceof HttpException) {
62
                $errorCode = $originError->getStatusCode();
63
                $formattedError['message'] = Response::$statusTexts[$errorCode] ?? $formattedError['message'];
64
                $formattedError['debugMessage'] = $originError->getMessage() ?: $formattedError['message'];
65
            }
66
67
            if ($originError instanceof ValidationError) {
68
                $formattedError['constraintViolations'] = $originError->getViolationsArray();
69
            }
70
71
            $trackingId = Uuid::createFromData(
72
                [
73
                    'message' => $originError->getMessage(),
74
                    'code' => $originError->getCode(),
75
                    'file' => $originError->getFile(),
76
                    'line' => $originError->getLine(),
77
                ]
78
            );
79
80
        } elseif ($originError instanceof FieldNode || $originError instanceof Schema) {
81
            $errorCode = Response::HTTP_BAD_REQUEST;
82
            $trackingId = Uuid::createFromData($formattedError['message'] ?? null);
83
        }
84
85
        $formattedError = array_merge(
86
            [
87
                'code' => $errorCode,
88
                'tracking_id' => $trackingId,
89
            ],
90
            $formattedError
91
        );
92
93
        return $formattedError;
94
    }
95
}
96