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

DefaultErrorFormatter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
eloc 33
dl 0
loc 70
ccs 0
cts 46
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C format() 0 50 12
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 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