Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -83,6 +84,7 @@ public function getConfigTreeBuilder()
->end()
->end()
->arrayNode('exceptions')
->addDefaultsIfNotSet()
->children()
->arrayNode('warnings')
->treatNullLike([])
Expand All @@ -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()
Expand Down
12 changes: 6 additions & 6 deletions DependencyInjection/OverblogGraphQLExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']])
;
}
}
Expand Down
38 changes: 31 additions & 7 deletions Error/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -66,19 +89,20 @@ 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());
}
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;
}
Expand All @@ -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,
Expand All @@ -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.',
Expand All @@ -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)
Expand Down