Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 79cc8d1

Browse files
authored
ENGCOM-5818: GraphQl-220: Implement exception logging #355
2 parents d93d288 + 91c57bb commit 79cc8d1

File tree

4 files changed

+106
-4
lines changed

4 files changed

+106
-4
lines changed

app/etc/graphql/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="Magento\Framework\GraphQl\Query\ErrorHandlerInterface" type="Magento\Framework\GraphQl\Query\ErrorHandler"/>
10+
</config>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
use GraphQL\Error\ClientAware;
11+
use Psr\Log\LoggerInterface;
12+
13+
/**
14+
* @inheritDoc
15+
*
16+
* @package Magento\Framework\GraphQl\Query
17+
*/
18+
class ErrorHandler implements ErrorHandlerInterface
19+
{
20+
/**
21+
* @var LoggerInterface
22+
*/
23+
private $logger;
24+
25+
/**
26+
* @param LoggerInterface $logger
27+
*/
28+
public function __construct(
29+
LoggerInterface $logger
30+
) {
31+
$this->logger = $logger;
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function handle(array $errors, callable $formatter): array
38+
{
39+
return array_map(
40+
function (ClientAware $error) use ($formatter) {
41+
$this->logger->error($error);
42+
43+
return $formatter($error);
44+
},
45+
$errors
46+
);
47+
}
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
use GraphQL\Error\Error;
11+
12+
/**
13+
* Interface ErrorHandlerInterface
14+
*
15+
* GraphQL error handler
16+
*
17+
* @see \Magento\Framework\GraphQl\Query\QueryProcessor
18+
*
19+
* @api
20+
*/
21+
interface ErrorHandlerInterface
22+
{
23+
/**
24+
* Handle errors
25+
*
26+
* @param Error[] $errors
27+
* @param callable $formatter
28+
*
29+
* @return array
30+
*/
31+
public function handle(array $errors, callable $formatter): array;
32+
}

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

Lines changed: 16 additions & 4 deletions
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 : false

0 commit comments

Comments
 (0)