Skip to content

Commit

Permalink
Implement exception logging #220.
Browse files Browse the repository at this point in the history
Created custom error handler;
Applied default error handling functionality
  • Loading branch information
Maksym Novik committed Feb 7, 2019
1 parent 8dfe26a commit 583ff11
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
<preference for="Magento\Framework\MessageQueue\Bulk\ExchangeFactoryInterface" type="Magento\Framework\MessageQueue\Bulk\ExchangeFactory" />
<preference for="Magento\Framework\MessageQueue\QueueFactoryInterface" type="Magento\Framework\MessageQueue\QueueFactory" />
<preference for="Magento\Framework\Search\Request\IndexScopeResolverInterface" type="Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver"/>
<preference for="\Magento\Framework\GraphQl\Query\ErrorHandlerInterface" type="\Magento\Framework\GraphQl\Query\ErrorHandler"/>
<type name="Magento\Framework\Model\ResourceModel\Db\TransactionManager" shared="false" />
<type name="Magento\Framework\Acl\Data\Cache">
<arguments>
Expand Down
29 changes: 29 additions & 0 deletions lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\GraphQl\Query;

/**
* Class ErrorHandler
*
* @package Magento\Framework\GraphQl\Query
*/
class ErrorHandler implements ErrorHandlerInterface
{
/**
* Handle errors
*
* @param \GraphQL\Error\Error[] $errors
* @param callable $formatter
*
* @return array
*/
public function handle(array $errors, callable $formatter):array
{
return array_map($formatter, $errors);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\GraphQl\Query;

/**
* Interface ErrorHandlerInterface
*
* @package Magento\Framework\GraphQl\Query
*/
interface ErrorHandlerInterface
{
/**
* Handle errors
*
* @param \GraphQL\Error\Error[] $errors
* @param callable $formatter
*
* @return array
*/
public function handle(array $errors, callable $formatter):array;
}
20 changes: 16 additions & 4 deletions lib/internal/Magento/Framework/GraphQl/Query/QueryProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace Magento\Framework\GraphQl\Query;

use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
use Magento\Framework\GraphQl\Schema;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Schema;

/**
* Wrapper for GraphQl execution of a schema
Expand All @@ -27,15 +27,25 @@ class QueryProcessor
private $queryComplexityLimiter;

/**
* @param ExceptionFormatter $exceptionFormatter
* @param QueryComplexityLimiter $queryComplexityLimiter
* @var \Magento\Framework\GraphQl\Query\ErrorHandlerInterface
*/
private $errorHandler;

/**
* @param ExceptionFormatter $exceptionFormatter
* @param QueryComplexityLimiter $queryComplexityLimiter
*
* @param \Magento\Framework\GraphQl\Query\ErrorHandlerInterface $errorHandler
* @SuppressWarnings(PHPMD.LongVariable)
*/
public function __construct(
ExceptionFormatter $exceptionFormatter,
QueryComplexityLimiter $queryComplexityLimiter
QueryComplexityLimiter $queryComplexityLimiter,
ErrorHandlerInterface $errorHandler
) {
$this->exceptionFormatter = $exceptionFormatter;
$this->queryComplexityLimiter = $queryComplexityLimiter;
$this->errorHandler = $errorHandler;
}

/**
Expand Down Expand Up @@ -67,6 +77,8 @@ public function process(
$contextValue,
$variableValues,
$operationName
)->setErrorsHandler(
[$this->errorHandler, 'handle']
)->toArray(
$this->exceptionFormatter->shouldShowDetail() ?
\GraphQL\Error\Debug::INCLUDE_DEBUG_MESSAGE | \GraphQL\Error\Debug::INCLUDE_TRACE : false
Expand Down

0 comments on commit 583ff11

Please sign in to comment.