|
13 | 13 | use StudioNet\GraphQL\Support\Definition\Definition;
|
14 | 14 | use GraphQL\Error\Error;
|
15 | 15 | use StudioNet\GraphQL\Error\ValidationError;
|
| 16 | +use GraphQL\Error\Debug; |
| 17 | +use GraphQL\Error\FormattedError; |
16 | 18 |
|
17 | 19 | /**
|
18 | 20 | * GraphQL implementation singleton
|
@@ -131,40 +133,37 @@ public function execute($query, $variables = [], $opts = []) {
|
131 | 133 | return $result;
|
132 | 134 | };
|
133 | 135 |
|
134 |
| - $data = GraphQLBase::executeQuery($schema, $query, $root, $context, $variables, $operation, $fieldResolver); |
135 |
| - |
136 |
| - if (!empty($data->errors)) { |
137 |
| - return [ |
138 |
| - 'data' => $data->data, |
139 |
| - 'errors' => array_map([$this, 'formatError'], $data->errors) |
140 |
| - ]; |
141 |
| - } |
142 |
| - |
143 |
| - return $data->toArray(true); |
| 136 | + // rethrow internal exception (will be catched in the controller) |
| 137 | + return GraphQLBase::executeQuery($schema, $query, $root, $context, $variables, $operation, $fieldResolver) |
| 138 | + ->toArray(Debug::RETHROW_INTERNAL_EXCEPTIONS); |
144 | 139 | }
|
145 | 140 |
|
146 | 141 | /**
|
147 |
| - * Format error |
| 142 | + * handle GraphQL exception |
148 | 143 | *
|
149 |
| - * @param Error $e |
| 144 | + * @param \Throwable $e |
150 | 145 | * @return array
|
| 146 | + * @static |
151 | 147 | */
|
152 |
| - protected function formatError(Error $e) { |
153 |
| - $error = ['message' => $e->getMessage()]; |
154 |
| - $locs = $e->getLocations(); |
155 |
| - $prev = $e->getPrevious(); |
| 148 | + static public function formatGraphQLException(\Throwable $e): array { |
| 149 | + $debug = false; |
156 | 150 |
|
157 |
| - if (!empty($locs)) { |
158 |
| - $error['locations'] = array_map(function ($loc) { return $loc->toArray(); }, $locs); |
| 151 | + // if debug mode is activated, we have to include debug message and |
| 152 | + // trace to easily found exception |
| 153 | + if (config('app.debug')) { |
| 154 | + $debug = DEBUG::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE; |
159 | 155 | }
|
160 | 156 |
|
161 |
| - if (!empty($prev)) { |
162 |
| - if ($prev instanceof ValidationError) { |
163 |
| - $error['validation'] = $prev->getValidatorMessages()->toArray(); |
164 |
| - } |
| 157 | + // create the array exception based on default GraphQL implementation |
| 158 | + $data = FormattedError::createFromException($e, $debug); |
| 159 | + |
| 160 | + // for validation error, we have to add new entry in exception to |
| 161 | + // know which field is wrong |
| 162 | + if ($e instanceof ValidationError) { |
| 163 | + $data['validation'] = $e->getValidatorMessages()->toArray(); |
165 | 164 | }
|
166 | 165 |
|
167 |
| - return $error; |
| 166 | + return $data; |
168 | 167 | }
|
169 | 168 |
|
170 | 169 | /**
|
|
0 commit comments