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\ControlledErrorInterface; |
19
|
|
|
use Ynlo\GraphQLBundle\Util\Uuid; |
20
|
|
|
|
21
|
|
|
class DefaultErrorFormatter implements ErrorFormatterInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @inheritDoc |
25
|
|
|
*/ |
26
|
1 |
|
public function format(Error $error, $debug = false): array |
27
|
|
|
{ |
28
|
1 |
|
$formattedError = FormattedError::createFromException($error, $debug); |
29
|
|
|
|
30
|
1 |
|
if (!isset($formattedError['tracking_id'])) { |
31
|
1 |
|
$originError = $error->getTrace()[0]['args'][0] ?? null; |
32
|
|
|
|
33
|
1 |
|
$trackingId = null; |
34
|
1 |
|
$errorCode = Response::HTTP_INTERNAL_SERVER_ERROR; |
35
|
1 |
|
if ($originError instanceof \Exception) { |
36
|
1 |
|
if ($originError instanceof ControlledErrorInterface) { |
37
|
|
|
$errorCode = $originError->getCode(); |
38
|
1 |
|
} elseif ($originError instanceof ClientAware && $originError->isClientSafe()) { |
39
|
1 |
|
$errorCode = null; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
$trackingId = Uuid::createFromData( |
43
|
|
|
[ |
44
|
1 |
|
'message' => $originError->getMessage(), |
45
|
1 |
|
'code' => $originError->getCode(), |
46
|
1 |
|
'file' => $originError->getFile(), |
47
|
1 |
|
'line' => $originError->getLine(), |
48
|
|
|
] |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
} elseif ($originError instanceof FieldNode) { |
52
|
|
|
$errorCode = Response::HTTP_BAD_REQUEST; |
53
|
|
|
$trackingId = Uuid::createFromData($formattedError['message'] ?? null); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$formattedError = array_merge( |
57
|
|
|
[ |
58
|
1 |
|
'code' => $errorCode, |
59
|
1 |
|
'tracking_id' => $trackingId, |
60
|
|
|
], |
61
|
1 |
|
$formattedError |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $formattedError; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|