From 1d3864353840c413797deef2fcb5b5023fcd642f Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Thu, 17 Aug 2017 03:01:23 +0700 Subject: [PATCH] Ability to re-throw resolver exceptions --- docs/error-handling.md | 9 +++++++++ docs/reference.md | 1 + src/Error/FormattedError.php | 11 ++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/docs/error-handling.md b/docs/error-handling.md index a4129ef..21e36bb 100644 --- a/docs/error-handling.md +++ b/docs/error-handling.md @@ -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. diff --git a/docs/reference.md b/docs/reference.md index 1379193..cba14c4 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -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); diff --git a/src/Error/FormattedError.php b/src/Error/FormattedError.php index 02c18b8..a67e07b 100644 --- a/src/Error/FormattedError.php +++ b/src/Error/FormattedError.php @@ -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;