Ability to re-throw resolver exceptions

This commit is contained in:
Vladimir Razuvaev 2017-08-17 03:01:23 +07:00
parent a2be92937e
commit 1d38643538
3 changed files with 20 additions and 1 deletions

View File

@ -109,6 +109,15 @@ This will make each error entry to look like this:
]
```
If you prefer first resolver exception to be re-thrown, use following flags:
```php
use GraphQL\Error\FormattedError;
$debug = FormattedError::INCLUDE_DEBUG_MESSAGE | FormattedError::RETHROW_RESOLVER_EXCEPTIONS;
// Following will throw if there was an exception in resolver during execution:
$result = GraphQL::executeQuery(/*args*/)->toArray($debug);
```
# Custom Error Handling and Formatting
It is possible to define custom **formatter** and **handler** for result errors.

View File

@ -871,6 +871,7 @@ class FormattedError
{
const INCLUDE_DEBUG_MESSAGE = 1;
const INCLUDE_TRACE = 2;
const RETHROW_RESOLVER_EXCEPTIONS = 4;
public static function setInternalErrorMessage($msg);

View File

@ -15,6 +15,7 @@ class FormattedError
{
const INCLUDE_DEBUG_MESSAGE = 1;
const INCLUDE_TRACE = 2;
const RETHROW_RESOLVER_EXCEPTIONS = 4;
private static $internalErrorMessage = 'Internal server error';
@ -30,8 +31,10 @@ class FormattedError
* @param \Throwable $e
* @param bool|int $debug
* @param string $internalErrorMessage
*
* @return array
* @throws Error
*
* @throws \Throwable
*/
public static function createFromException($e, $debug = false, $internalErrorMessage = null)
{
@ -41,6 +44,12 @@ class FormattedError
Utils::getVariableType($e)
);
if ($debug & self::RETHROW_RESOLVER_EXCEPTIONS > 0) {
if (!$e instanceof Error || $e->getPrevious()) {
throw $e;
}
}
$debug = (int) $debug;
$internalErrorMessage = $internalErrorMessage ?: self::$internalErrorMessage;