Skip to content

Commit 583ff11

Browse files
author
Maksym Novik
committed
Implement exception logging #220.
Created custom error handler; Applied default error handling functionality
1 parent 8dfe26a commit 583ff11

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

app/etc/di.xml

+1
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
<preference for="Magento\Framework\MessageQueue\Bulk\ExchangeFactoryInterface" type="Magento\Framework\MessageQueue\Bulk\ExchangeFactory" />
210210
<preference for="Magento\Framework\MessageQueue\QueueFactoryInterface" type="Magento\Framework\MessageQueue\QueueFactory" />
211211
<preference for="Magento\Framework\Search\Request\IndexScopeResolverInterface" type="Magento\Framework\Indexer\ScopeResolver\IndexScopeResolver"/>
212+
<preference for="\Magento\Framework\GraphQl\Query\ErrorHandlerInterface" type="\Magento\Framework\GraphQl\Query\ErrorHandler"/>
212213
<type name="Magento\Framework\Model\ResourceModel\Db\TransactionManager" shared="false" />
213214
<type name="Magento\Framework\Acl\Data\Cache">
214215
<arguments>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\GraphQl\Query;
9+
10+
/**
11+
* Class ErrorHandler
12+
*
13+
* @package Magento\Framework\GraphQl\Query
14+
*/
15+
class ErrorHandler implements ErrorHandlerInterface
16+
{
17+
/**
18+
* Handle errors
19+
*
20+
* @param \GraphQL\Error\Error[] $errors
21+
* @param callable $formatter
22+
*
23+
* @return array
24+
*/
25+
public function handle(array $errors, callable $formatter):array
26+
{
27+
return array_map($formatter, $errors);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\GraphQl\Query;
9+
10+
/**
11+
* Interface ErrorHandlerInterface
12+
*
13+
* @package Magento\Framework\GraphQl\Query
14+
*/
15+
interface ErrorHandlerInterface
16+
{
17+
/**
18+
* Handle errors
19+
*
20+
* @param \GraphQL\Error\Error[] $errors
21+
* @param callable $formatter
22+
*
23+
* @return array
24+
*/
25+
public function handle(array $errors, callable $formatter):array;
26+
}

lib/internal/Magento/Framework/GraphQl/Query/QueryProcessor.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
namespace Magento\Framework\GraphQl\Query;
99

1010
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
11-
use Magento\Framework\GraphQl\Schema;
1211
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
12+
use Magento\Framework\GraphQl\Schema;
1313

1414
/**
1515
* Wrapper for GraphQl execution of a schema
@@ -27,15 +27,25 @@ class QueryProcessor
2727
private $queryComplexityLimiter;
2828

2929
/**
30-
* @param ExceptionFormatter $exceptionFormatter
31-
* @param QueryComplexityLimiter $queryComplexityLimiter
30+
* @var \Magento\Framework\GraphQl\Query\ErrorHandlerInterface
31+
*/
32+
private $errorHandler;
33+
34+
/**
35+
* @param ExceptionFormatter $exceptionFormatter
36+
* @param QueryComplexityLimiter $queryComplexityLimiter
37+
*
38+
* @param \Magento\Framework\GraphQl\Query\ErrorHandlerInterface $errorHandler
39+
* @SuppressWarnings(PHPMD.LongVariable)
3240
*/
3341
public function __construct(
3442
ExceptionFormatter $exceptionFormatter,
35-
QueryComplexityLimiter $queryComplexityLimiter
43+
QueryComplexityLimiter $queryComplexityLimiter,
44+
ErrorHandlerInterface $errorHandler
3645
) {
3746
$this->exceptionFormatter = $exceptionFormatter;
3847
$this->queryComplexityLimiter = $queryComplexityLimiter;
48+
$this->errorHandler = $errorHandler;
3949
}
4050

4151
/**
@@ -67,6 +77,8 @@ public function process(
6777
$contextValue,
6878
$variableValues,
6979
$operationName
80+
)->setErrorsHandler(
81+
[$this->errorHandler, 'handle']
7082
)->toArray(
7183
$this->exceptionFormatter->shouldShowDetail() ?
7284
\GraphQL\Error\Debug::INCLUDE_DEBUG_MESSAGE | \GraphQL\Error\Debug::INCLUDE_TRACE : false

0 commit comments

Comments
 (0)