diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ac675ef3c..ac468b87f 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -13,6 +13,7 @@ use GraphQL\Validator\Rules\QueryComplexity; use GraphQL\Validator\Rules\QueryDepth; +use Overblog\GraphQLBundle\Error\ErrorHandler; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -83,6 +84,7 @@ public function getConfigTreeBuilder() ->end() ->end() ->arrayNode('exceptions') + ->addDefaultsIfNotSet() ->children() ->arrayNode('warnings') ->treatNullLike([]) @@ -96,10 +98,10 @@ public function getConfigTreeBuilder() ->addDefaultsIfNotSet() ->children() ->scalarNode('warnings') - ->defaultValue('Overblog\\GraphQLBundle\\Error\\UserWarning') + ->defaultValue(ErrorHandler::DEFAULT_USER_WARNING_CLASS) ->end() ->scalarNode('errors') - ->defaultValue('Overblog\\GraphQLBundle\\Error\\UserError') + ->defaultValue(ErrorHandler::DEFAULT_USER_ERROR_CLASS) ->end() ->end() ->end() diff --git a/DependencyInjection/OverblogGraphQLExtension.php b/DependencyInjection/OverblogGraphQLExtension.php index 5644c2928..e0fdc2ce6 100644 --- a/DependencyInjection/OverblogGraphQLExtension.php +++ b/DependencyInjection/OverblogGraphQLExtension.php @@ -99,17 +99,17 @@ private function setGraphiQLTemplate(array $config, ContainerBuilder $container) private function setErrorHandlerArguments(array $config, ContainerBuilder $container) { + $errorHandlerDefinition = $container->getDefinition($this->getAlias().'.error_handler'); + if (isset($config['definitions']['internal_error_message'])) { - $container - ->getDefinition($this->getAlias().'.error_handler') - ->replaceArgument(0, $config['definitions']['internal_error_message']) - ; + $errorHandlerDefinition->replaceArgument(0, $config['definitions']['internal_error_message']); } if (isset($config['definitions']['exceptions'])) { - $container - ->getDefinition($this->getAlias().'.error_handler') + $errorHandlerDefinition ->replaceArgument(2, $this->buildExceptionMap($config['definitions']['exceptions'])) + ->addMethodCall('setUserWarningClass', [$config['definitions']['exceptions']['types']['warnings']]) + ->addMethodCall('setUserErrorClass', [$config['definitions']['exceptions']['types']['errors']]) ; } } diff --git a/Error/ErrorHandler.php b/Error/ErrorHandler.php index fb610da23..4dd178ed5 100644 --- a/Error/ErrorHandler.php +++ b/Error/ErrorHandler.php @@ -15,10 +15,13 @@ use GraphQL\Executor\ExecutionResult; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; +use Psr\Log\LogLevel; class ErrorHandler { const DEFAULT_ERROR_MESSAGE = 'Internal server Error'; + const DEFAULT_USER_WARNING_CLASS = 'Overblog\\GraphQLBundle\\Error\\UserWarning'; + const DEFAULT_USER_ERROR_CLASS = 'Overblog\\GraphQLBundle\\Error\\UserError'; /** @var LoggerInterface */ private $logger; @@ -29,6 +32,12 @@ class ErrorHandler /** @var array */ private $exceptionMap; + /** @var string */ + private $userWarningClass = self::DEFAULT_USER_WARNING_CLASS; + + /** @var string */ + private $userErrorClass = self::DEFAULT_USER_ERROR_CLASS; + public function __construct($internalErrorMessage = null, LoggerInterface $logger = null, array $exceptionMap = []) { $this->logger = (null === $logger) ? new NullLogger() : $logger; @@ -39,6 +48,20 @@ public function __construct($internalErrorMessage = null, LoggerInterface $logge $this->exceptionMap = $exceptionMap; } + public function setUserWarningClass($userWarningClass) + { + $this->userWarningClass = $userWarningClass; + + return $this; + } + + public function setUserErrorClass($userErrorClass) + { + $this->userErrorClass = $userErrorClass; + + return $this; + } + /** * @param Error[] $errors * @param bool $throwRawException @@ -66,7 +89,8 @@ protected function treatExceptions(array $errors, $throwRawException) continue; } - if ($rawException instanceof UserError) { + // user error + if ($rawException instanceof $this->userErrorClass) { $treatedExceptions['errors'][] = $error; if ($rawException->getPrevious()) { $this->logException($rawException->getPrevious()); @@ -74,11 +98,11 @@ protected function treatExceptions(array $errors, $throwRawException) continue; } - // user warnings - if ($rawException instanceof UserWarning) { + // user warning + if ($rawException instanceof $this->userWarningClass) { $treatedExceptions['extensions']['warnings'][] = $error; if ($rawException->getPrevious()) { - $this->logException($rawException->getPrevious()); + $this->logException($rawException->getPrevious(), LogLevel::WARNING); } continue; } @@ -97,7 +121,7 @@ protected function treatExceptions(array $errors, $throwRawException) throw $rawException; } - $this->logException($rawException); + $this->logException($rawException, LogLevel::CRITICAL); $treatedExceptions['errors'][] = new Error( $this->internalErrorMessage, @@ -111,7 +135,7 @@ protected function treatExceptions(array $errors, $throwRawException) return $treatedExceptions; } - public function logException(\Exception $exception) + public function logException(\Exception $exception, $errorLevel = LogLevel::ERROR) { $message = sprintf( '%s: %s[%d] (caught exception) at %s line %s.', @@ -122,7 +146,7 @@ public function logException(\Exception $exception) $exception->getLine() ); - $this->logger->error($message, ['exception' => $exception]); + $this->logger->$errorLevel($message, ['exception' => $exception]); } public function handleErrors(ExecutionResult $executionResult, $throwRawException = false)