Skip to content

Commit 6a683c4

Browse files
author
Cyril Mizzi
committed
fix(exception): handle exception properly
ref #25
1 parent 40f409c commit 6a683c4

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/GraphQL.php

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use StudioNet\GraphQL\Support\Definition\Definition;
1414
use GraphQL\Error\Error;
1515
use StudioNet\GraphQL\Error\ValidationError;
16+
use GraphQL\Error\Debug;
17+
use GraphQL\Error\FormattedError;
1618

1719
/**
1820
* GraphQL implementation singleton
@@ -131,40 +133,37 @@ public function execute($query, $variables = [], $opts = []) {
131133
return $result;
132134
};
133135

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);
144139
}
145140

146141
/**
147-
* Format error
142+
* handle GraphQL exception
148143
*
149-
* @param Error $e
144+
* @param \Throwable $e
150145
* @return array
146+
* @static
151147
*/
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;
156150

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;
159155
}
160156

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();
165164
}
166165

167-
return $error;
166+
return $data;
168167
}
169168

170169
/**

src/GraphQLController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function query(Request $request, $schema = null) {
4848
$data = $this->executeQuery($schema, $inputs);
4949
}
5050
} catch (\Exception $exception) {
51-
$data['errors'] = $exception->getMessage();
51+
$data = \GraphQL::formatGraphQLException($exception);
5252
Log::debug($exception);
5353
}
5454

0 commit comments

Comments
 (0)