Skip to content

Commit a084270

Browse files
author
Jeremiah VALERIE
committed
Add UserErrors to help sending multiple errors to user
1 parent adf5f56 commit a084270

File tree

3 files changed

+114
-26
lines changed

3 files changed

+114
-26
lines changed

Error/ErrorHandler.php

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,45 @@ public function __construct($internalErrorMessage = null, LoggerInterface $logge
4444
*/
4545
private function getDefaultErrorHandler()
4646
{
47-
return function (Error $error, $throwRawException) {
48-
$rawException = $error->getPrevious();
49-
50-
if (null === $rawException) {
51-
return $error;
52-
}
53-
if ($rawException instanceof UserError) {
54-
return $error;
55-
}
56-
57-
// if is a try catch exception wrapped in Error
58-
if ($throwRawException) {
59-
throw $rawException;
47+
return function (array $errors, $throwRawException) {
48+
$clean = [];
49+
50+
/** @var Error $error */
51+
foreach($errors as $error) {
52+
$rawException = $error->getPrevious();
53+
54+
// Parse error or user error
55+
if (null === $rawException || $rawException instanceof UserError) {
56+
$clean[] = $error;
57+
continue;
58+
}
59+
60+
// multiple errors
61+
if ($rawException instanceof UserErrors) {
62+
$rawExceptions = $rawException;
63+
foreach($rawExceptions->getErrors() as $rawException) {
64+
$clean[] = Error::createLocatedError($rawException, $error->nodes);
65+
}
66+
continue;
67+
}
68+
69+
// if is a try catch exception wrapped in Error
70+
if ($throwRawException) {
71+
throw $rawException;
72+
}
73+
74+
$this->logException($rawException);
75+
76+
$clean[] = new Error(
77+
$this->internalErrorMessage,
78+
$error->nodes,
79+
$rawException,
80+
$error->getSource(),
81+
$error->getPositions()
82+
);
6083
}
6184

62-
$this->logException($rawException);
63-
64-
return new Error(
65-
$this->internalErrorMessage,
66-
$error->nodes,
67-
$rawException,
68-
$error->getSource(),
69-
$error->getPositions()
70-
);
85+
return $clean;
7186
};
7287
}
7388

@@ -101,8 +116,6 @@ public function setErrorHandler(callable $errorHandler)
101116

102117
public function handleErrors(ExecutionResult $executionResult, $throwRawException = false)
103118
{
104-
foreach ($executionResult->errors as $i => $error) {
105-
$executionResult->errors[$i] = call_user_func_array($this->errorHandler, [$error, $throwRawException]);
106-
}
119+
$executionResult->errors = call_user_func_array($this->errorHandler, [$executionResult->errors, $throwRawException]);
107120
}
108121
}

Error/UserErrors.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
/**
15+
* Class UserErrors.
16+
*/
17+
class UserErrors extends \Exception
18+
{
19+
/**
20+
* @var UserError[]
21+
*/
22+
private $errors = [];
23+
24+
public function __construct(array $errors, $message = "", $code = 0, \Exception $previous = null)
25+
{
26+
$this->setErrors($errors);
27+
parent::__construct($message, $code, $previous);
28+
}
29+
30+
/**
31+
* @param UserError[]|string[] $errors
32+
*/
33+
public function setErrors(array $errors)
34+
{
35+
foreach ($errors as $error) {
36+
$this->addError($error);
37+
}
38+
}
39+
40+
/**
41+
* @param string|UserError $error
42+
* @return $this
43+
*/
44+
public function addError($error)
45+
{
46+
if (is_string($error)) {
47+
$error = new UserError($error);
48+
} elseif(!is_object($error) || !$error instanceof UserError) {
49+
throw new \InvalidArgumentException('Error must be string or instance of Overblog\\GraphQLBundle\\Error\\UserError.');
50+
}
51+
52+
$this->errors[] = $error;
53+
54+
return $this;
55+
}
56+
57+
/**
58+
* @return UserError[]
59+
*/
60+
public function getErrors()
61+
{
62+
return $this->errors;
63+
}
64+
}

Tests/Error/ErrorHandlerTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use GraphQL\Executor\ExecutionResult;
1616
use Overblog\GraphQLBundle\Error\ErrorHandler;
1717
use Overblog\GraphQLBundle\Error\UserError;
18+
use Overblog\GraphQLBundle\Error\UserErrors;
1819

1920
class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
2021
{
@@ -34,6 +35,7 @@ public function testMaskErrorWithThrowExceptionSetToFalse()
3435
new Error('Error without wrapped exception'),
3536
new Error('Error with wrapped exception', null, new \Exception('My Exception message')),
3637
new Error('Error with wrapped user error', null, new UserError('My User Error')),
38+
new Error('', null, new UserErrors(['My User Error 1', 'My User Error 2', new UserError('My User Error 3')])),
3739
]
3840
);
3941

@@ -51,6 +53,15 @@ public function testMaskErrorWithThrowExceptionSetToFalse()
5153
[
5254
'message' => 'Error with wrapped user error',
5355
],
56+
[
57+
'message' => 'My User Error 1',
58+
],
59+
[
60+
'message' => 'My User Error 2',
61+
],
62+
[
63+
'message' => 'My User Error 3',
64+
],
5465
],
5566
];
5667

@@ -129,7 +140,7 @@ public function testMaskErrorOverrideErrorHandle()
129140
);
130141

131142
$this->errorHandler->setErrorHandler(function () {
132-
return new Error('Override Error');
143+
return [new Error('Override Error')];
133144
});
134145

135146
$this->errorHandler->handleErrors($executionResult);

0 commit comments

Comments
 (0)