|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the OverblogGraphQLBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Overblog <http://github.com/overblog/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Overblog\GraphQLBundle\Error; |
|
13
|
|
|
|
|
14
|
|
|
use GraphQL\Error; |
|
15
|
|
|
use GraphQL\Executor\ExecutionResult; |
|
16
|
|
|
use Psr\Log\LoggerInterface; |
|
17
|
|
|
use Psr\Log\NullLogger; |
|
18
|
|
|
|
|
19
|
|
|
class ErrorHandler |
|
20
|
|
|
{ |
|
21
|
|
|
const DEFAULT_ERROR_MESSAGE = 'Internal server Error'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var LoggerInterface */ |
|
24
|
|
|
private $logger; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $internalErrorMessage; |
|
28
|
|
|
|
|
29
|
29 |
|
public function __construct($internalErrorMessage = null, LoggerInterface $logger = null) |
|
30
|
|
|
{ |
|
31
|
29 |
|
$this->logger = (null === $logger) ? new NullLogger() : $logger; |
|
32
|
29 |
|
if (empty($internalErrorMessage)) { |
|
33
|
29 |
|
$internalErrorMessage = self::DEFAULT_ERROR_MESSAGE; |
|
34
|
29 |
|
} |
|
35
|
29 |
|
$this->internalErrorMessage = $internalErrorMessage; |
|
36
|
29 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Error[] $errors |
|
40
|
|
|
* @param $throwRawException |
|
41
|
|
|
* |
|
42
|
|
|
* @return Error[] |
|
43
|
|
|
* |
|
44
|
|
|
* @throws \Exception |
|
45
|
|
|
*/ |
|
46
|
29 |
|
protected function treatErrors(array $errors, $throwRawException) |
|
47
|
|
|
{ |
|
48
|
29 |
|
$treatedErrors = []; |
|
49
|
|
|
|
|
50
|
|
|
/** @var Error $error */ |
|
51
|
29 |
|
foreach ($errors as $error) { |
|
52
|
5 |
|
$rawException = $error->getPrevious(); |
|
53
|
|
|
|
|
54
|
|
|
// Parse error or user error |
|
55
|
5 |
|
if (null === $rawException || $rawException instanceof UserError) { |
|
56
|
4 |
|
$treatedErrors[] = $error; |
|
57
|
4 |
|
continue; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// multiple errors |
|
61
|
2 |
|
if ($rawException instanceof UserErrors) { |
|
62
|
1 |
|
$rawExceptions = $rawException; |
|
63
|
1 |
|
foreach ($rawExceptions->getErrors() as $rawException) { |
|
64
|
1 |
|
$treatedErrors[] = Error::createLocatedError($rawException, $error->nodes); |
|
65
|
1 |
|
} |
|
66
|
1 |
|
continue; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// if is a try catch exception wrapped in Error |
|
70
|
2 |
|
if ($throwRawException) { |
|
71
|
1 |
|
throw $rawException; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$this->logException($rawException); |
|
75
|
|
|
|
|
76
|
1 |
|
$treatedErrors[] = new Error( |
|
77
|
1 |
|
$this->internalErrorMessage, |
|
78
|
1 |
|
$error->nodes, |
|
79
|
1 |
|
$rawException, |
|
80
|
1 |
|
$error->getSource(), |
|
81
|
1 |
|
$error->getPositions() |
|
82
|
1 |
|
); |
|
83
|
28 |
|
} |
|
84
|
|
|
|
|
85
|
28 |
|
return $treatedErrors; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
public function logException(\Exception $exception) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$message = sprintf( |
|
91
|
1 |
|
'%s: %s[%d] (uncaught exception) at %s line %s.', |
|
92
|
1 |
|
get_class($exception), |
|
93
|
1 |
|
$exception->getMessage(), |
|
94
|
1 |
|
$exception->getCode(), |
|
95
|
1 |
|
$exception->getFile(), |
|
96
|
1 |
|
$exception->getLine() |
|
97
|
1 |
|
); |
|
98
|
|
|
|
|
99
|
1 |
|
$this->logger->error($message, ['exception' => $exception]); |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
29 |
|
public function handleErrors(ExecutionResult $executionResult, $throwRawException = false) |
|
103
|
|
|
{ |
|
104
|
29 |
|
$executionResult->errors = $this->treatErrors($executionResult->errors, $throwRawException); |
|
105
|
28 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|