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
|
43 |
|
public function __construct($internalErrorMessage = null, LoggerInterface $logger = null) |
30
|
|
|
{ |
31
|
43 |
|
$this->logger = (null === $logger) ? new NullLogger() : $logger; |
32
|
43 |
|
if (empty($internalErrorMessage)) { |
33
|
43 |
|
$internalErrorMessage = self::DEFAULT_ERROR_MESSAGE; |
34
|
43 |
|
} |
35
|
43 |
|
$this->internalErrorMessage = $internalErrorMessage; |
36
|
43 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Error[] $errors |
40
|
|
|
* @param bool $throwRawException |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
* |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
43 |
|
protected function treatExceptions(array $errors, $throwRawException) |
47
|
|
|
{ |
48
|
|
|
$treatedExceptions = [ |
49
|
43 |
|
'errors' => [], |
50
|
|
|
'extensions' => [ |
51
|
43 |
|
'warnings' => [], |
52
|
43 |
|
], |
53
|
43 |
|
]; |
54
|
|
|
|
55
|
|
|
/** @var Error $error */ |
56
|
43 |
|
foreach ($errors as $error) { |
57
|
10 |
|
$rawException = $error->getPrevious(); |
58
|
|
|
|
59
|
|
|
// Parse error or user error |
60
|
10 |
|
if (null === $rawException) { |
61
|
4 |
|
$treatedExceptions['errors'][] = $error; |
62
|
4 |
|
continue; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
7 |
|
if ($rawException instanceof UserError) { |
66
|
2 |
|
$treatedExceptions['errors'][] = $error; |
67
|
2 |
|
if ($rawException->getPrevious()) { |
68
|
|
|
$this->logException($rawException->getPrevious()); |
69
|
|
|
} |
70
|
2 |
|
continue; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// user warnings |
74
|
6 |
|
if ($rawException instanceof UserWarning) { |
75
|
5 |
|
$treatedExceptions['extensions']['warnings'][] = $error; |
76
|
5 |
|
if ($rawException->getPrevious()) { |
77
|
|
|
$this->logException($rawException->getPrevious()); |
78
|
|
|
} |
79
|
5 |
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// multiple errors |
83
|
2 |
|
if ($rawException instanceof UserErrors) { |
84
|
1 |
|
$rawExceptions = $rawException; |
85
|
1 |
|
foreach ($rawExceptions->getErrors() as $rawException) { |
86
|
1 |
|
$treatedExceptions['errors'][] = Error::createLocatedError($rawException, $error->nodes); |
87
|
1 |
|
} |
88
|
1 |
|
continue; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// if is a try catch exception wrapped in Error |
92
|
2 |
|
if ($throwRawException) { |
93
|
1 |
|
throw $rawException; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$this->logException($rawException); |
97
|
|
|
|
98
|
1 |
|
$treatedExceptions['errors'][] = new Error( |
99
|
1 |
|
$this->internalErrorMessage, |
100
|
1 |
|
$error->nodes, |
101
|
1 |
|
$rawException, |
102
|
1 |
|
$error->getSource(), |
103
|
1 |
|
$error->getPositions() |
104
|
1 |
|
); |
105
|
42 |
|
} |
106
|
|
|
|
107
|
42 |
|
return $treatedExceptions; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function logException(\Exception $exception) |
111
|
|
|
{ |
112
|
1 |
|
$message = sprintf( |
113
|
1 |
|
'%s: %s[%d] (caught exception) at %s line %s.', |
114
|
1 |
|
get_class($exception), |
115
|
1 |
|
$exception->getMessage(), |
116
|
1 |
|
$exception->getCode(), |
117
|
1 |
|
$exception->getFile(), |
118
|
1 |
|
$exception->getLine() |
119
|
1 |
|
); |
120
|
|
|
|
121
|
1 |
|
$this->logger->error($message, ['exception' => $exception]); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
43 |
|
public function handleErrors(ExecutionResult $executionResult, $throwRawException = false) |
125
|
|
|
{ |
126
|
43 |
|
$exceptions = $this->treatExceptions($executionResult->errors, $throwRawException); |
127
|
42 |
|
$executionResult->errors = $exceptions['errors']; |
128
|
42 |
|
if (!empty($exceptions['extensions']['warnings'])) { |
129
|
5 |
|
$executionResult->extensions['warnings'] = array_map(['GraphQL\Error', 'formatError'], $exceptions['extensions']['warnings']); |
130
|
5 |
|
} |
131
|
42 |
|
} |
132
|
|
|
} |
133
|
|
|
|