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

Commit

Permalink
ENGCOM-5818: GraphQl-220: Implement exception logging #355
Browse files Browse the repository at this point in the history
  • Loading branch information
lenaorobei authored Sep 9, 2019
2 parents d93d288 + 91c57bb commit 79cc8d1
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 4 deletions.
10 changes: 10 additions & 0 deletions app/etc/graphql/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Framework\GraphQl\Query\ErrorHandlerInterface" type="Magento\Framework\GraphQl\Query\ErrorHandler"/>
</config>
48 changes: 48 additions & 0 deletions lib/internal/Magento/Framework/GraphQl/Query/ErrorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\GraphQl\Query;

use GraphQL\Error\ClientAware;
use Psr\Log\LoggerInterface;

/**
* @inheritDoc
*
* @package Magento\Framework\GraphQl\Query
*/
class ErrorHandler implements ErrorHandlerInterface
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @param LoggerInterface $logger
*/
public function __construct(
LoggerInterface $logger
) {
$this->logger = $logger;
}

/**
* @inheritDoc
*/
public function handle(array $errors, callable $formatter): array
{
return array_map(
function (ClientAware $error) use ($formatter) {
$this->logger->error($error);

return $formatter($error);
},
$errors
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\GraphQl\Query;

use GraphQL\Error\Error;

/**
* Interface ErrorHandlerInterface
*
* GraphQL error handler
*
* @see \Magento\Framework\GraphQl\Query\QueryProcessor
*
* @api
*/
interface ErrorHandlerInterface
{
/**
* Handle errors
*
* @param 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 : false
Expand Down

0 comments on commit 79cc8d1

Please sign in to comment.