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
|
|
|
/** @var array */ |
30
|
|
|
private $exceptionMap; |
31
|
|
|
|
32
|
13 |
|
public function __construct($internalErrorMessage = null, LoggerInterface $logger = null, array $exceptionMap = []) |
33
|
|
|
{ |
34
|
13 |
|
$this->logger = (null === $logger) ? new NullLogger() : $logger; |
35
|
13 |
|
if (empty($internalErrorMessage)) { |
36
|
12 |
|
$internalErrorMessage = self::DEFAULT_ERROR_MESSAGE; |
37
|
12 |
|
} |
38
|
13 |
|
$this->internalErrorMessage = $internalErrorMessage; |
39
|
13 |
|
$this->exceptionMap = $exceptionMap; |
40
|
13 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Error[] $errors |
44
|
|
|
* @param bool $throwRawException |
45
|
|
|
* |
46
|
|
|
* @return array |
47
|
|
|
* |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
45 |
|
protected function treatExceptions(array $errors, $throwRawException) |
51
|
|
|
{ |
52
|
|
|
$treatedExceptions = [ |
53
|
45 |
|
'errors' => [], |
54
|
|
|
'extensions' => [ |
55
|
45 |
|
'warnings' => [], |
56
|
45 |
|
], |
57
|
45 |
|
]; |
58
|
|
|
|
59
|
|
|
/** @var Error $error */ |
60
|
45 |
|
foreach ($errors as $error) { |
61
|
12 |
|
$rawException = $this->convertException($error->getPrevious()); |
62
|
|
|
|
63
|
|
|
// Parse error or user error |
64
|
12 |
|
if (null === $rawException) { |
65
|
4 |
|
$treatedExceptions['errors'][] = $error; |
66
|
4 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
9 |
|
if ($rawException instanceof UserError) { |
70
|
4 |
|
$treatedExceptions['errors'][] = $error; |
71
|
4 |
|
if ($rawException->getPrevious()) { |
72
|
1 |
|
$this->logException($rawException->getPrevious()); |
73
|
1 |
|
} |
74
|
4 |
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// user warnings |
78
|
6 |
|
if ($rawException instanceof UserWarning) { |
79
|
5 |
|
$treatedExceptions['extensions']['warnings'][] = $error; |
80
|
5 |
|
if ($rawException->getPrevious()) { |
81
|
1 |
|
$this->logException($rawException->getPrevious()); |
82
|
1 |
|
} |
83
|
5 |
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// multiple errors |
87
|
2 |
|
if ($rawException instanceof UserErrors) { |
88
|
1 |
|
$rawExceptions = $rawException; |
89
|
1 |
|
foreach ($rawExceptions->getErrors() as $rawException) { |
90
|
1 |
|
$treatedExceptions['errors'][] = Error::createLocatedError($rawException, $error->nodes); |
91
|
1 |
|
} |
92
|
1 |
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// if is a try catch exception wrapped in Error |
96
|
2 |
|
if ($throwRawException) { |
97
|
1 |
|
throw $rawException; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$this->logException($rawException); |
101
|
|
|
|
102
|
1 |
|
$treatedExceptions['errors'][] = new Error( |
103
|
1 |
|
$this->internalErrorMessage, |
104
|
1 |
|
$error->nodes, |
105
|
1 |
|
$rawException, |
106
|
1 |
|
$error->getSource(), |
107
|
1 |
|
$error->getPositions() |
108
|
1 |
|
); |
109
|
44 |
|
} |
110
|
|
|
|
111
|
44 |
|
return $treatedExceptions; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
public function logException(\Exception $exception) |
115
|
|
|
{ |
116
|
3 |
|
$message = sprintf( |
117
|
3 |
|
'%s: %s[%d] (caught exception) at %s line %s.', |
118
|
3 |
|
get_class($exception), |
119
|
3 |
|
$exception->getMessage(), |
120
|
3 |
|
$exception->getCode(), |
121
|
3 |
|
$exception->getFile(), |
122
|
3 |
|
$exception->getLine() |
123
|
3 |
|
); |
124
|
|
|
|
125
|
3 |
|
$this->logger->error($message, ['exception' => $exception]); |
126
|
3 |
|
} |
127
|
|
|
|
128
|
45 |
|
public function handleErrors(ExecutionResult $executionResult, $throwRawException = false) |
129
|
|
|
{ |
130
|
45 |
|
$exceptions = $this->treatExceptions($executionResult->errors, $throwRawException); |
131
|
44 |
|
$executionResult->errors = $exceptions['errors']; |
132
|
44 |
|
if (!empty($exceptions['extensions']['warnings'])) { |
133
|
5 |
|
$executionResult->extensions['warnings'] = array_map(['GraphQL\Error', 'formatError'], $exceptions['extensions']['warnings']); |
134
|
5 |
|
} |
135
|
44 |
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Tries to convert a raw exception into a user warning or error |
139
|
|
|
* that is displayed to the user. |
140
|
|
|
* |
141
|
|
|
* @param \Exception $rawException |
142
|
|
|
* @return \Exception |
143
|
|
|
*/ |
144
|
12 |
|
protected function convertException(\Exception $rawException = null) |
145
|
|
|
{ |
146
|
12 |
|
if (empty($rawException)) { |
147
|
4 |
|
return $rawException; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$types = [ |
151
|
9 |
|
'warnings' => 'Overblog\\GraphQLBundle\\Error\\UserWarning', |
152
|
9 |
|
'errors' => 'Overblog\\GraphQLBundle\\Error\\UserError', |
153
|
9 |
|
]; |
154
|
|
|
|
155
|
9 |
|
foreach ($types as $type => $errorClass) { |
156
|
9 |
|
if (!empty($this->exceptionMap[$type]) |
157
|
9 |
|
&& in_array(get_class($rawException), $this->exceptionMap[$type])) { |
158
|
2 |
|
return new $errorClass($rawException->getMessage(), $rawException->getCode(), $rawException); |
159
|
|
|
} |
160
|
8 |
|
} |
161
|
|
|
|
162
|
7 |
|
return $rawException; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|