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\Language\AST\FieldNode; |
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
|
16
|
|
|
class DefaultErrorHandler implements ErrorHandlerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var LoggerInterface|null |
20
|
|
|
*/ |
21
|
|
|
protected $logger; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* DefaultErrorHandler constructor. |
25
|
|
|
* |
26
|
|
|
* @param LoggerInterface $logger |
27
|
|
|
*/ |
28
|
22 |
|
public function __construct(?LoggerInterface $logger = null) |
29
|
|
|
{ |
30
|
22 |
|
$this->logger = $logger; |
31
|
22 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritDoc |
35
|
|
|
*/ |
36
|
1 |
|
public function handle(array $errors, ErrorFormatterInterface $errorFormatter, $debug = false): array |
37
|
|
|
{ |
38
|
1 |
|
$formattedErrors = []; |
39
|
1 |
|
foreach ($errors as $error) { |
40
|
1 |
|
$formattedError = $errorFormatter->format($error, $debug); |
41
|
|
|
|
42
|
1 |
|
if (null !== $this->logger) { |
43
|
1 |
|
$originError = $error->getTrace()[0]['args'][0] ?? null; |
44
|
|
|
|
45
|
1 |
|
$context = []; |
46
|
1 |
|
if ($originError instanceof \Exception) { |
47
|
|
|
$context = [ |
48
|
1 |
|
'file' => $originError->getFile(), |
49
|
1 |
|
'line' => $originError->getLine(), |
50
|
1 |
|
'error' => get_class($originError), |
51
|
1 |
|
'trace' => $originError->getTraceAsString(), |
52
|
|
|
]; |
53
|
|
|
} |
54
|
1 |
|
$message = $error->getMessage(); |
55
|
1 |
|
if ($trackingId = $formattedError['tracking_id'] ?? null) { |
56
|
1 |
|
$message = sprintf('(%s) %s', $trackingId, $message); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
if ($originError instanceof FieldNode) { |
60
|
|
|
$this->logger->debug($message, $context); //graphql error |
61
|
1 |
|
} elseif ($error->isClientSafe()) { |
62
|
1 |
|
$this->logger->notice($message, $context); // client aware error |
63
|
|
|
} else { |
64
|
|
|
$this->logger->critical($message, $context); //unhandled exception |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$formattedErrors[] = $formattedError; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
return $formattedErrors; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|